diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-03-17 22:55:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-17 22:55:04 +0100 |
| commit | 5eb3433ed5201c6180e6bee26c3156fea4b174f0 (patch) | |
| tree | 3ff7e0997602c0125422710d7479a576e7f15ad8 /compiler/rustc_parse/src/parser/path.rs | |
| parent | a8956e6618370ab766a3c3c79c73a1f1f41130a6 (diff) | |
| parent | 9a6532276ec45e22695f0a012a0782c97bf39a2d (diff) | |
| download | rust-5eb3433ed5201c6180e6bee26c3156fea4b174f0.tar.gz rust-5eb3433ed5201c6180e6bee26c3156fea4b174f0.zip | |
Rollup merge of #94731 - TaKO8Ki:const-generic-expr-recovery, r=davidtwco,oli-obk
Suggest adding `{ .. }` around a const function call with arguments
closes #91020
Diffstat (limited to 'compiler/rustc_parse/src/parser/path.rs')
| -rw-r--r-- | compiler/rustc_parse/src/parser/path.rs | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 5e537d7b95c..17c57867cf9 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -624,9 +624,18 @@ impl<'a> Parser<'a> { GenericArg::Const(self.parse_const_arg()?) } else if self.check_type() { // Parse type argument. + let is_const_fn = self.look_ahead(1, |t| t.kind == token::OpenDelim(token::Paren)); + let mut snapshot = self.create_snapshot_for_diagnostic(); match self.parse_ty() { Ok(ty) => GenericArg::Type(ty), Err(err) => { + if is_const_fn { + if let Ok(expr) = (*snapshot).parse_expr_res(Restrictions::CONST_EXPR, None) + { + self.restore_snapshot(snapshot); + return Ok(Some(self.dummy_const_arg_needs_braces(err, expr.span))); + } + } // Try to recover from possible `const` arg without braces. return self.recover_const_arg(start, err).map(Some); } @@ -636,7 +645,7 @@ impl<'a> Parser<'a> { } else { // Fall back by trying to parse a const-expr expression. If we successfully do so, // then we should report an error that it needs to be wrapped in braces. - let snapshot = self.clone(); + let snapshot = self.create_snapshot_for_diagnostic(); match self.parse_expr_res(Restrictions::CONST_EXPR, None) { Ok(expr) => { return Ok(Some(self.dummy_const_arg_needs_braces( @@ -645,7 +654,7 @@ impl<'a> Parser<'a> { ))); } Err(err) => { - *self = snapshot; + self.restore_snapshot(snapshot); err.cancel(); return Ok(None); } |
