jjpe
March 10, 2022, 12:04pm
1
While looking at the source code for Option::ok_or() I saw a trait bound that looks like E: ~const Drop
.
My question is, what does the ~const
part mean? I'm assuming it's nightly-only since the rustc_const_unstable
attribute on top of the method says as much.
1 Like
alice
March 10, 2022, 12:08pm
2
4 Likes
Wow, that’s a weird syntax, I didn’t even know that exists. (I did know of ~const Trait
, but not of this weird abuse of “~const Drop
” notation). The compiler apparently isn’t fully aware of the abuse of notation here either; it actually thinks that T: ~const Drop
implies T: Drop
, and unsoundness entails
opened 01:22PM - 10 Mar 22 UTC
closed 06:59AM - 13 Apr 22 UTC
T-compiler
I-unsound
A-const-fn
C-bug
requires-nightly
F-const_trait_impl
```rs
#![feature(const_trait_impl)]
fn extend_lifetime(x: &str) -> &'static … str {
f::<S>(x)
}
const fn f<'a, T: Tr<'a> + ~const Drop>(x: &'a str) -> T::Ty {
// can call g with T: Drop bound, even though
// T: ~const Drop works for non-Drop-implementing types
g::<T>(x)
}
#[allow(drop_bounds)]
const fn g<T: Drop>(x: &str) -> <T as Tr<'_>>::Ty {
x
}
struct S;
trait Tr<'a> {
type Ty;
}
#[allow(drop_bounds)]
impl<'a, T: Drop> Tr<'a> for T {
type Ty = &'a str;
}
impl<'a> Tr<'a> for S {
type Ty = &'static str;
}
fn main() {
let x = "Hello World".to_owned();
let s = extend_lifetime(&x);
drop(x);
println!("{s}");
}
```
```
ďż˝)
```
([playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=90858e7c355c9f6859d180bd415de0dd))
@rustbot label requires-nightly, I-unsound, T-compiler, F-const_trait_impl, A-const-fn
By the way, even if this worked “correctly” (i.e. soundly) I ***really*** hate the fact that this feature is using a sort of `T: Drop` bound; the `drop_bounds` warning exists for a reason, using the `Drop` trait in bounds is discouraged for a reason, abusing it as in the feature that allows “`T: ~const Drop`” makes everything just sooo much more confusing and inconsistent.
9 Likes
And here’s a second issue I found looking into const Drop
right now
opened 01:58PM - 10 Mar 22 UTC
closed 08:51PM - 10 Mar 22 UTC
P-high
regression-from-stable-to-stable
T-libs
```rs
const fn f<T>(_: Box<T>) {}
```
([playground](https://play.rust-lang.or… g/?version=stable&mode=debug&edition=2021&gist=6349e1b7d3023327e25c2fdebf4924cb))
Note that this works even if `T` implements `Drop` in a non-`const` way. This seems ***hightly*** problematic, especially provided we eventually want to support constructing `Box` at compile time.
Relevant PR: [#91884](https://github.com/rust-lang/rust/pull/91884/files#diff-ee1616b052da71e1a0b80b66544fc07b98debc288e71eeb3e827c7fc3e7e9ecfR1173-R1174)
Of course that impl *claims* to be unstable, but as we all know trait implementations are *never* unstable, unless the trait is unstable. (For ordinary traits, implementations of `const Trait` are thus unstable merely due to the fact that no user can use any such implementations in stable code yet.)
@rustbot label regression-from-stable-to-stable
~~I don’t know what team to tag.~~
The fix should be straightforward: Remove the `const Drop for Box<T, A>` implementation (i.e. revert it to be non-`const`), and add a test.
3 Likes
H2CO3
March 10, 2022, 2:51pm
5
Oh, that truly looks awful.
system
Closed
June 8, 2022, 2:52pm
6
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.