-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustc/rustc_mir: Implement RFC 2203. #61749
rustc/rustc_mir: Implement RFC 2203. #61749
Conversation
src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs
Outdated
Show resolved
Hide resolved
src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs
Outdated
Show resolved
Hide resolved
src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs
Outdated
Show resolved
Hide resolved
0ec77d9
to
fd8fc14
Compare
This comment has been minimized.
This comment has been minimized.
.span_label(span, &format!( | ||
"the trait `{}` is not implemented for `{}`", | ||
copy_path, ty, | ||
)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @nikomatsakis @matthewjasper This is not a proper trait error, so it would miss the "backtrace" that one might get otherwise - any suggestions on how to fix that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started re-using some existing functions for diagnostics in this PR. It appears to work correctly but it might not produce the full "backtrace" that you want.
To expand on #61749 (comment), this example: #[derive(Copy, Clone)]
struct Foo<T>(T);
fn main() {
[Foo(String::new()); 4];
} Currently errors with: error[E0277]: the trait bound `std::string::String: std::marker::Copy` is not satisfied
--> src/main.rs:4:5
|
4 | [Foo(String::new()); 4];
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
|
= note: required because of the requirements on the impl of `std::marker::Copy` for `Foo<std::string::String>`
= note: the `Copy` trait is required because the repeated element will be copied The "required because of the requirements on the impl of ..." part would now be missing, I think. |
I've added this example as a test. |
@davidtwco Sadly, without adding a test like this (or a more complex one) on master first, it's hard to see the changes in diagnostics. |
This comment has been minimized.
This comment has been minimized.
@eddyb I've ran that example with another working directory (at most 24 hours old behind master) and this is the current error:
|
3c7ee48
to
99ebd9a
Compare
This comment has been minimized.
This comment has been minimized.
55546ac
to
fbf884b
Compare
fbf884b
to
72ec9d7
Compare
src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-const-fn-pass.rs
Outdated
Show resolved
Hide resolved
This comment has been minimized.
This comment has been minimized.
b9cae48
to
9d0ce71
Compare
This comment has been minimized.
This comment has been minimized.
9d0ce71
to
2c10ded
Compare
This comment has been minimized.
This comment has been minimized.
2c10ded
to
4a185c0
Compare
What is this blocked on, if anything? |
I'm not aware of any outstanding concerns, only that it isn't a proper trait error with a backtrace being emitted. |
This comment has been minimized.
This comment has been minimized.
4a185c0
to
a357743
Compare
This comment has been minimized.
This comment has been minimized.
8168d22
to
4b1bc2d
Compare
r? @matthewjasper or @nikomatsakis - We need a way to report proper trait errors, with their cause/origin backtraces. |
@davidtwco I'm sorry for blocking this, I don't think the diagnostic regressions are worth the wait. |
@@ -3,11 +3,12 @@ | |||
|
|||
fn f<T: Copy, const N: usize>(x: T) -> [T; N] { | |||
[x; {N}] | |||
//~^ ERROR array lengths can't depend on generic parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh I don't remember this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bors r+ p=2 rollup=never |
📌 Commit 4b1bc2d has been approved by |
☀️ Test successful - checks-azure |
| | ||
= help: the following implementations were found: | ||
<std::option::Option<T> as std::marker::Copy> | ||
= note: the `Copy` trait is required because the repeated element will be copied |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't mention the feature gate :(
)) = self.body[bb].statements[stmt_idx].kind { | ||
promoted_temps.insert(index); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized, this is unnecessary, only Candidate::Ref
cares (it removes StorageDead
from the temp that would need to be 'static
).
_, | ||
box Rvalue::Ref(_, _, Place::Base(PlaceBase::Local(index))) | ||
) = self.body[bb].statements[stmt_idx].kind { | ||
promoted_temps.insert(index); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oli-obk Wow this is wrong?! const FOO: &i32 = { let x = &(5, false).0; x };
fails because of it, lol.
…error, r=ecstatic-morse suggest `const_in_array_repeat_expression` flag This PR adds a suggestion to add the `#![feature(const_in_array_repeat_expression)]` attribute to the crate when a promotable expression is used in a repeat expression and the feature gate is not enabled. Unfortunately, this ended up being a little bit more complex than I anticipated, which may not have been worth it given that this would all be removed when the feature is stabilized. However, with rust-lang#65732 and rust-lang#65737 being open, and the feature gate having not been being suggested to potential users, the feature might not be stabilized in a while, so maybe this is worth landing. cc @Centril (addresses [this comment](rust-lang#61749 (comment))) r? @ecstatic-morse (opened issues related to RFC 2203 recently)
let arr: [Option<String>; 2] = [None::<String>; 2]; | ||
//~^ ERROR the trait bound `std::option::Option<std::string::String>: std::marker::Copy` is not satisfied [E0277] | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out this test was not thorough enough, and some other cases sneaked past the feature gate.
Acknowledge that `[CONST; N]` is stable When `const_in_array_repeat_expressions` (RFC 2203) got unstably implemented as part of rust-lang#61749, accidentally, the special case of repeating a *constant* got stabilized immediately. That is why the following code works on stable: ```rust const EMPTY: Vec<i32> = Vec::new(); pub const fn bar() -> [Vec<i32>; 2] { [EMPTY; 2] } fn main() { let x = bar(); } ``` In contrast, if we had written `[expr; 2]` for some expression that is not *literally* a constant but could be evaluated at compile-time (e.g. `(EMPTY,).0`), this would have failed. We could take back this stabilization as it was clearly accidental. However, I propose we instead just officially accept this and stabilize a small subset of RFC 2203, while leaving the more complex case of general expressions that could be evaluated at compile-time unstable. Making that case work well is pretty much blocked on inline `const` expressions (to avoid relying too much on [implicit promotion](https://github.com/rust-lang/const-eval/blob/master/promotion.md)), so it could take a bit until it comes to full fruition. `[CONST; N]` is an uncontroversial subset of this feature that has no semantic ambiguities, does not rely on promotion, and basically provides the full expressive power of RFC 2203 but without the convenience (people have to define constants to repeat them, possibly using associated consts if generics are involved). Well, I said "no semantic ambiguities", that is only almost true... the one point I am not sure about is `[CONST; 0]`. There are two possible behaviors here: either this is equivalent to `let x = CONST; [x; 0]`, or it is a NOP (if we argue that the constant is never actually instantiated). The difference between the two is that if `CONST` has a destructor, it should run in the former case (but currently doesn't, due to rust-lang#74836); but should not run if it is considered a NOP. For regular `[x; 0]` there seems to be consensus on running drop (there isn't really an alternative); any opinions for the `CONST` special case? Should this instantiate the const only to immediately run its destructors? That seems somewhat silly to me. After all, the `let`-expansion does *not* work in general, for `N > 1`. Cc `@rust-lang/lang` `@rust-lang/wg-const-eval` Cc rust-lang#49147
This PR implements RFC 2203, allowing constants in array repeat
expressions. Part of #49147.
r? @eddyb