-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Introduce const Trait
(always-const trait bounds)
#119099
Conversation
This comment has been minimized.
This comment has been minimized.
let id = lcx.next_node_id(); | ||
let hir_id = lcx.next_id(); |
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 moved this {Node,Hir}Id
creation below the HirId
registration for the hir::ExprKind::Path
above to accommodate the control flow, I hope that doesn't break any invariants (?).
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.
No, as long as you didn't move it outside of a nested self.with_*
function.
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.
Actually... hm... it's probably fine...
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.
Do we have tests to check that we deny ~const
in const items and structs and stuff?
|
b265a78
to
1ab36f6
Compare
@@ -786,6 +794,7 @@ impl<'a> Parser<'a> { | |||
|| self.check(&token::Not) | |||
|| self.check(&token::Question) | |||
|| self.check(&token::Tilde) | |||
|| self.check_keyword(kw::Const) |
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 need to investigate if this visibly changes the MBE matching behavior for stable users. I hope not since this check is required for correctness.
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.
Doesn't seem to be a problem. I've added a test to ensure that const Trait
doesn't regress stable code: tests/ui/rfcs/rfc-2632-const-trait-impl/mbe-bare-trait-objects-const-trait-bounds.rs
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.
It's obvious in hindsight but this doesn't regress the aforementioned test because I haven't (and I won't) add kw::Const
& TokenKind::Tilde
to can_begin_type
.
This comment has been minimized.
This comment has been minimized.
1ab36f6
to
932c309
Compare
Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt |
This comment has been minimized.
This comment has been minimized.
310df46
to
2abd167
Compare
☔ The latest upstream changes (presumably #119163) made this pull request unmergeable. Please resolve the merge conflicts. |
self.sess.emit_err(errors::ModifierLifetime { | ||
span, | ||
modifier: "const", | ||
padding: " ", | ||
}); |
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 did not see an ui test for the padding suggestion. Could you give me a link to it or add it?
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.
Well, padding
didn't actually make sense. I've removed it. I could run-rustfix
for the -Zparse-only
test tests/ui/parser/bounds-type.rs
but I'm not sure if it's worth it esp. since it concerns a maybe-incorrect suggestion.
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.
No need for run-rustfix, just an ui test that would have covered this code path with stderr would be fine. Though removing it also works.
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.
It's a tool-only suggestion atm, hence it doesn't show up on stderr.
…=compiler-errors Clean up `check_consts` and misc fixes 1. Remove most of the logic around erroring with trait methods. I have kept the part resolving it to a concrete impl, as that is used for const stability checks. 2. Turning on `effects` causes ICE with generic args, due to `~const Tr` when `Tr` is not `#[const_trait]` tripping up expectation in code that handles generic args, more specifically here: https://github.com/rust-lang/rust/blob/8681e077b8afa99d60acf8f8470a012a3ce709a5/compiler/rustc_hir_analysis/src/astconv/generics.rs#L377 We set `arg_count.correct` to `Err` to correctly signal that an error has already been reported. 3. UI test blesses. Edit(fmease): Fixes rust-lang#117244 (UI test is in rust-lang#119099 for now). r? compiler-errors
…ompiler-errors Clean up `check_consts` and misc fixes 1. Remove most of the logic around erroring with trait methods. I have kept the part resolving it to a concrete impl, as that is used for const stability checks. 2. Turning on `effects` causes ICE with generic args, due to `~const Tr` when `Tr` is not `#[const_trait]` tripping up expectation in code that handles generic args, more specifically here: https://github.com/rust-lang/rust/blob/8681e077b8afa99d60acf8f8470a012a3ce709a5/compiler/rustc_hir_analysis/src/astconv/generics.rs#L377 We set `arg_count.correct` to `Err` to correctly signal that an error has already been reported. 3. UI test blesses. Edit(fmease): Fixes rust-lang#117244 (UI test is in rust-lang#119099 for now). r? compiler-errors
💔 Test failed - checks-actions |
Network error |
☀️ Test successful - checks-actions |
Finished benchmarking commit (88d69b7): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 670.98s -> 672.366s (0.21%) |
…, r=compiler-errors Pretty-print always-const trait predicates correctly Follow-up to rust-lang#119099. r? fee1-dead
…, r=compiler-errors Pretty-print always-const trait predicates correctly Follow-up to rust-lang#119099. r? fee1-dead
…, r=compiler-errors Pretty-print always-const trait predicates correctly Follow-up to rust-lang#119099. r? fee1-dead
Rollup merge of rust-lang#119476 - fmease:pp-always-const-trait-preds, r=compiler-errors Pretty-print always-const trait predicates correctly Follow-up to rust-lang#119099. r? fee1-dead
@fmease @compiler-errors @fee1-dead I'm hoping I can get some help. I've bisected rust-lang/rustfmt#6035 back to this PR. With the following input: rustfmt 1.7.0-dev (88d69b7 2023-12-27) outputs: m!(const N: usize = 0;); but the commit just before (rustfmt 1.7.0-dev (a861c89 2023-12-27)) outputs: m!(
const N: usize = 0;
); Any idea why the formatting would have changed? |
…=calebcartwright,ytmimi rustfmt: Check that a token can begin a nonterminal kind before parsing it as a macro arg r? `@ytmimi` and/or `@calebcartwright` cc `@fmease` I'm putting this on r-l/rust since it should fix the nightly rustfmt version. If you don't care about having this regression until the next rustfmt->rust sync, then I can move that PR over to r-l/rustfmt. --- > Any idea why the formatting would have changed [from rust-lang#119099]? **Copied over explanation:** This has to do with the weirdness of the way that `parse_macro_arg` works. Unlike parsing nonterminal args in a macro-by-example, it eagerly tries, for example, to parse a type without checking that the beginning token may begin a type: https://github.com/rust-lang/rustfmt/blob/bf967319e258acb9b1648a952bba52665eceaf52/src/parse/macros/mod.rs#L54 Contrast this to the nonterminal parsing code, which first checks that the nonterminal may begin with a given token: https://github.com/rust-lang/rust/blob/ef71f1047e04438181d7cb925a833e2ada6ab390/compiler/rustc_parse/src/parser/nonterminal.rs#L47 In rust-lang#119099, `@fmease` implemented a change so that `const Tr` would be parsed as `dyn const Tr` (a trait object to a const trait) in edition 2015. This is okay for the purposes of macros, because he explicitly made sure that `const` did not get added to the list of tokens that may begin a `:ty` nonterminal kind: rust-lang#119099 (comment) However, since rustfmt is not so careful about eagerly parsing macro args before checking that they're legal in macro position, this changed the way that the string of tokens is being parsed into macro args.
…=calebcartwright,ytmimi rustfmt: Check that a token can begin a nonterminal kind before parsing it as a macro arg r? ``@ytmimi`` and/or ``@calebcartwright`` cc ``@fmease`` I'm putting this on r-l/rust since it should fix the nightly rustfmt version. If you don't care about having this regression until the next rustfmt->rust sync, then I can move that PR over to r-l/rustfmt. --- > Any idea why the formatting would have changed [from rust-lang#119099]? **Copied over explanation:** This has to do with the weirdness of the way that `parse_macro_arg` works. Unlike parsing nonterminal args in a macro-by-example, it eagerly tries, for example, to parse a type without checking that the beginning token may begin a type: https://github.com/rust-lang/rustfmt/blob/bf967319e258acb9b1648a952bba52665eceaf52/src/parse/macros/mod.rs#L54 Contrast this to the nonterminal parsing code, which first checks that the nonterminal may begin with a given token: https://github.com/rust-lang/rust/blob/ef71f1047e04438181d7cb925a833e2ada6ab390/compiler/rustc_parse/src/parser/nonterminal.rs#L47 In rust-lang#119099, ``@fmease`` implemented a change so that `const Tr` would be parsed as `dyn const Tr` (a trait object to a const trait) in edition 2015. This is okay for the purposes of macros, because he explicitly made sure that `const` did not get added to the list of tokens that may begin a `:ty` nonterminal kind: rust-lang#119099 (comment) However, since rustfmt is not so careful about eagerly parsing macro args before checking that they're legal in macro position, this changed the way that the string of tokens is being parsed into macro args.
Rollup merge of rust-lang#120218 - compiler-errors:parse_macro_arg, r=calebcartwright,ytmimi rustfmt: Check that a token can begin a nonterminal kind before parsing it as a macro arg r? ``@ytmimi`` and/or ``@calebcartwright`` cc ``@fmease`` I'm putting this on r-l/rust since it should fix the nightly rustfmt version. If you don't care about having this regression until the next rustfmt->rust sync, then I can move that PR over to r-l/rustfmt. --- > Any idea why the formatting would have changed [from rust-lang#119099]? **Copied over explanation:** This has to do with the weirdness of the way that `parse_macro_arg` works. Unlike parsing nonterminal args in a macro-by-example, it eagerly tries, for example, to parse a type without checking that the beginning token may begin a type: https://github.com/rust-lang/rustfmt/blob/bf967319e258acb9b1648a952bba52665eceaf52/src/parse/macros/mod.rs#L54 Contrast this to the nonterminal parsing code, which first checks that the nonterminal may begin with a given token: https://github.com/rust-lang/rust/blob/ef71f1047e04438181d7cb925a833e2ada6ab390/compiler/rustc_parse/src/parser/nonterminal.rs#L47 In rust-lang#119099, ``@fmease`` implemented a change so that `const Tr` would be parsed as `dyn const Tr` (a trait object to a const trait) in edition 2015. This is okay for the purposes of macros, because he explicitly made sure that `const` did not get added to the list of tokens that may begin a `:ty` nonterminal kind: rust-lang#119099 (comment) However, since rustfmt is not so careful about eagerly parsing macro args before checking that they're legal in macro position, this changed the way that the string of tokens is being parsed into macro args.
internal: Parse (nightly) `const` and `async` trait bounds Both of these bound modifiers were added recently: * `const` trait bounds: rust-lang/rust#119099 * `async` trait bounds: rust-lang/rust#120392 The latter will certainly will not do the right thing; namely, `async Fn` needs to be mapped to the `AsyncFn` trait. IDK how to do that, so advice would be appreciated, though perhaps we could land this first so the parser isn't complaining about these bounds?
Feature
const_trait_impl
currently lacks a way to express “always const” trait bounds. This makes it impossible to define generic items like fns or structs which contain types that depend on const method calls (*). While the final design and esp. the syntax of effects / keyword generics isn't set in stone, some version of “always const” trait bounds will very likely form a part of it. Further, their implementation is trivial thanks to theeffects
backbone.Not sure if this needs t-lang sign-off though.
(*):
Lastly, “always const” trait bounds are a perfect fit for
generic_const_items
.Previously, we (oli, fee1-dead and I) wanted to reinterpret
~const Trait
asconst Trait
in generic const items which would've been quite surprising and not very generalizable.Supersedes #117530.
cc @oli-obk
As discussed
r? fee1-dead (or compiler)