-
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
parser: recover on for<'a> |...| body
closures
#70209
Conversation
/// Recover on an explicitly quantified closure expression, e.g., `for<'a> |x: &'a u8| *x + 1`. | ||
fn recover_quantified_closure_expr(&mut self, attrs: AttrVec) -> PResult<'a, P<Expr>> { | ||
let lo = self.token.span; | ||
let _ = self.parse_late_bound_lifetime_defs()?; |
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 guess what I was worried about is this any early exist from here resulting in a for
loop successfully parsing but I guess that's not possible.
let span_for = lo.to(self.prev_token.span); | ||
let closure = self.parse_closure_expr(attrs)?; | ||
|
||
self.struct_span_err(span_for, "cannot introduce explicit parameters for a closure") |
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 wonder if the language here could be more focused on the present rather than sounding absolute. Like "not yet supported" but maybe not suggesting future support is certain?
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.
We tend to shy away from "not yet" so as to not suggest that it will happen; "cannot" is a fairly standard start of a diagnostic message in the compiler.
cc @petrochenkov @nikomatsakis I don't want to unilaterally approve this, but I've been wanting make sure we don't paint ourselves into a corner wrt generic closure syntax (i.e. by it being ambiguous with |
622dbc5
to
13eb4df
Compare
I'm mildly skeptical about this, the syntax is unlikely to be written accidentally, so there's not much motivation for doing this for recovery. Regarding future proofing, we already have ambiguities " |
13eb4df
to
4d30b92
Compare
Switched over to As for the for-loop thing. Keeping in mind that these must be irrefutable patterns using |
@bors r+ |
📌 Commit 4d30b92 has been approved by |
…trochenkov parser: recover on `for<'a> |...| body` closures When encountering `for` and `<` is 1 token ahead, interpret this as an explicitly quantified generic closure and recover, rather than attempting to parse a `for` loop. This provides both improved diagnostics as well as an insurance policy for the ability to use this as the syntax for generic closures in the future. As requested by r? @eddyb
Rollup of 10 pull requests Successful merges: - rust-lang#68099 (Amend Rc/Arc::from_raw() docs regarding unsafety) - rust-lang#70172 (parse/lexer: support `StringReader::retokenize` called on external files.) - rust-lang#70209 (parser: recover on `for<'a> |...| body` closures) - rust-lang#70223 (fix type of const params in associated types.) - rust-lang#70229 (more clippy fixes) - rust-lang#70240 (Return NonZeroU64 from ThreadId::as_u64.) - rust-lang#70250 (Remove wrong entry from RELEASES.md) - rust-lang#70253 (Remove another wrong entry from RELEASES.md) - rust-lang#70254 (couple more clippy fixes (let_and_return, if_same_then_else)) - rust-lang#70266 (proc_macro_harness: Use item header spans for errors) Failed merges: r? @ghost
Is it ok that this artificial example no longer compiles in nightly? https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c444d791d838b73c5ad3e9813e9838c6 #[derive(PartialEq, Eq)]
struct A;
impl A {
const A: A = A;
}
fn main() {
for <A>::A in vec![A] {}
} |
So, let me understand what happened here:
|
for future reference, linking rust-lang/rfcs#3216 |
When encountering
for
and<
is 1 token ahead, interpret this as an explicitly quantified generic closure and recover, rather than attempting to parse afor
loop. This provides both improved diagnostics as well as an insurance policy for the ability to use this as the syntax for generic closures in the future.As requested by r? @eddyb