File tree 5 files changed +18
-28
lines changed
compiler/rustc_parse/src/parser
src/test/ui/const-generics
5 files changed +18
-28
lines changed Original file line number Diff line number Diff line change @@ -1808,9 +1808,13 @@ impl<'a> Parser<'a> {
1808
1808
return Ok ( false ) ; // Don't continue.
1809
1809
}
1810
1810
1811
- /// Handle a generic const argument that had not been enclosed in braces, and suggest enclosing
1812
- /// it braces. In this situation, unlike in `handle_ambiguous_unbraced_const_arg`, this is
1813
- /// almost certainly a const argument, so we always offer a suggestion.
1811
+ /// Attempt to parse a generic const argument that has not been enclosed in braces.
1812
+ /// There are a limited number of expressions that are permitted without being encoded
1813
+ /// in braces:
1814
+ /// - Literals.
1815
+ /// - Single-segment paths (i.e. standalone generic const parameters).
1816
+ /// All other expressions that can be parsed will emit an error suggesting the expression be
1817
+ /// wrapped in braces.
1814
1818
pub fn handle_unambiguous_unbraced_const_arg ( & mut self ) -> PResult < ' a , P < Expr > > {
1815
1819
let start = self . token . span ;
1816
1820
let expr = self . parse_expr_res ( Restrictions :: CONST_EXPR , None ) . map_err ( |mut err| {
Original file line number Diff line number Diff line change @@ -489,13 +489,21 @@ impl<'a> Parser<'a> {
489
489
/// - An expression surrounded in `{}`.
490
490
/// - A literal.
491
491
/// - A numeric literal prefixed by `-`.
492
+ /// - A single-segment path.
492
493
pub ( super ) fn expr_is_valid_const_arg ( & self , expr : & P < rustc_ast:: Expr > ) -> bool {
493
494
match & expr. kind {
494
495
ast:: ExprKind :: Block ( _, _) | ast:: ExprKind :: Lit ( _) => true ,
495
496
ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , expr) => match & expr. kind {
496
497
ast:: ExprKind :: Lit ( _) => true ,
497
498
_ => false ,
498
499
} ,
500
+ // We can only resolve single-segment paths at the moment, because multi-segment paths
501
+ // require type-checking: see `visit_generic_arg` in `src/librustc_resolve/late.rs`.
502
+ ast:: ExprKind :: Path ( None , path)
503
+ if path. segments . len ( ) == 1 && path. segments [ 0 ] . args . is_none ( ) =>
504
+ {
505
+ true
506
+ }
499
507
_ => false ,
500
508
}
501
509
}
Original file line number Diff line number Diff line change 1
- error: expressions must be enclosed in braces to be used as const generic arguments
2
- --> $DIR/macro_rules-braces.rs:34:17
3
- |
4
- LL | let _: baz!(N);
5
- | ^
6
- |
7
- help: enclose the `const` expression in braces
8
- |
9
- LL | let _: baz!({ N });
10
- | ^ ^
11
-
12
1
error: expressions must be enclosed in braces to be used as const generic arguments
13
2
--> $DIR/macro_rules-braces.rs:54:17
14
3
|
@@ -68,5 +57,5 @@ LL | let _: biz!({ N });
68
57
= note: this may fail depending on what value the parameter takes
69
58
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
70
59
71
- error: aborting due to 6 previous errors
60
+ error: aborting due to 5 previous errors
72
61
Original file line number Diff line number Diff line change 1
- error: expressions must be enclosed in braces to be used as const generic arguments
2
- --> $DIR/macro_rules-braces.rs:34:17
3
- |
4
- LL | let _: baz!(N);
5
- | ^
6
- |
7
- help: enclose the `const` expression in braces
8
- |
9
- LL | let _: baz!({ N });
10
- | ^ ^
11
-
12
1
error: expressions must be enclosed in braces to be used as const generic arguments
13
2
--> $DIR/macro_rules-braces.rs:54:17
14
3
|
@@ -52,5 +41,5 @@ LL | let _: biz!({ N });
52
41
|
53
42
= help: const parameters may only be used as standalone arguments, i.e. `N`
54
43
55
- error: aborting due to 6 previous errors
44
+ error: aborting due to 5 previous errors
56
45
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ fn test<const N: usize>() {
31
31
let _: foo ! ( { { N } } ) ; //[min]~ ERROR generic parameters may not
32
32
let _: bar ! ( N ) ;
33
33
let _: bar ! ( { N } ) ; //[min]~ ERROR generic parameters may not
34
- let _: baz ! ( N ) ; //~ ERROR expressions must be enclosed in braces
34
+ let _: baz ! ( N ) ;
35
35
let _: baz ! ( { N } ) ;
36
36
let _: baz ! ( { { N } } ) ; //[min]~ ERROR generic parameters may not
37
37
let _: biz ! ( N ) ;
You can’t perform that action at this time.
0 commit comments