Skip to content

Commit 5127108

Browse files
committed
mGCA: drop literal anon-const special case in parser
1 parent d13924c commit 5127108

10 files changed

Lines changed: 34 additions & 62 deletions

compiler/rustc_parse/src/parser/path.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -919,24 +919,10 @@ impl<'a> Parser<'a> {
919919
Ok((expr, mgca_disambiguation))
920920
}
921921

922-
/// Under `min_generic_const_args` we still allow *some* anon consts to be written without
923-
/// a `const` block as it makes things quite a lot nicer. This function is useful for contexts
924-
/// where we would like to use `MgcaDisambiguation::Direct` but need to fudge it to be `AnonConst`
925-
/// in the presence of literals.
926-
//
927-
/// FIXME(min_generic_const_args): In the long term it would be nice to have a way to directly
928-
/// represent literals in `hir::ConstArgKind` so that we can remove this special case by not
929-
/// needing an anon const.
930-
pub fn mgca_direct_lit_hack(&self, expr: &Expr) -> MgcaDisambiguation {
931-
match &expr.kind {
932-
ast::ExprKind::Lit(_) => MgcaDisambiguation::AnonConst,
933-
ast::ExprKind::Unary(ast::UnOp::Neg, expr)
934-
if matches!(expr.kind, ast::ExprKind::Lit(_)) =>
935-
{
936-
MgcaDisambiguation::AnonConst
937-
}
938-
_ => MgcaDisambiguation::Direct,
939-
}
922+
/// Under `min_generic_const_args`, prefer direct const arguments rather than
923+
/// wrapping literals in anon consts.
924+
pub fn mgca_direct_lit_hack(&self, _expr: &Expr) -> MgcaDisambiguation {
925+
MgcaDisambiguation::Direct
940926
}
941927

942928
/// Parse a generic argument in a path segment.
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1-
error[E0277]: the trait bound `FreshTy(0): A` is not satisfied
1+
error: type annotations needed for the literal
22
--> $DIR/dyn-compat-self-const-projections-in-assoc-const-ty.rs:32:33
33
|
44
LL | let _: dyn A<Ty = i32, CT = 0>;
5-
| ^ the trait `A` is not implemented for `FreshTy(0)`
6-
|
7-
help: the trait `A` is implemented for `()`
8-
--> $DIR/dyn-compat-self-const-projections-in-assoc-const-ty.rs:20:1
9-
|
10-
LL | impl A for () {
11-
| ^^^^^^^^^^^^^
5+
| ^
126

13-
error[E0277]: the trait bound `FreshTy(0): A` is not satisfied
7+
error: type annotations needed for the literal
148
--> $DIR/dyn-compat-self-const-projections-in-assoc-const-ty.rs:34:34
159
|
1610
LL | let _: &dyn A<Ty = i32, CT = 0> = &();
17-
| ^ the trait `A` is not implemented for `FreshTy(0)`
18-
|
19-
help: the trait `A` is implemented for `()`
20-
--> $DIR/dyn-compat-self-const-projections-in-assoc-const-ty.rs:20:1
21-
|
22-
LL | impl A for () {
23-
| ^^^^^^^^^^^^^
11+
| ^
2412

2513
error: aborting due to 2 previous errors
2614

27-
For more information about this error, try `rustc --explain E0277`.

tests/ui/const-generics/mgca/array-expr-type-mismatch-in-where-bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where
1414

1515
fn bar<T>()
1616
where
17-
T: Trait2<3>, //~ ERROR: mismatched types
17+
T: Trait2<3>, //~ ERROR: type annotations needed for the literal
1818
{
1919
}
2020

tests/ui/const-generics/mgca/array-expr-type-mismatch-in-where-bound.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ error: expected `usize`, found const array
44
LL | T: Trait1<{ [] }>,
55
| ^^
66

7-
error[E0308]: mismatched types
7+
error: type annotations needed for the literal
88
--> $DIR/array-expr-type-mismatch-in-where-bound.rs:17:15
99
|
1010
LL | T: Trait2<3>,
11-
| ^ expected `[u8; 3]`, found integer
11+
| ^
1212

1313
error: aborting due to 2 previous errors
1414

15-
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/mgca/type_const-inherent-const-omitted-type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct A;
66
impl A {
77
type const B = 4;
88
//~^ ERROR: missing type for `const` item
9+
//~| ERROR: type annotations needed for the literal
910
}
1011

1112
fn main() {}

tests/ui/const-generics/mgca/type_const-inherent-const-omitted-type.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@ help: provide a type for the item
99
LL | type const B: <type> = 4;
1010
| ++++++++
1111

12-
error: aborting due to 1 previous error
12+
error: type annotations needed for the literal
13+
--> $DIR/type_const-inherent-const-omitted-type.rs:7:20
14+
|
15+
LL | type const B = 4;
16+
| ^
17+
18+
error: aborting due to 2 previous errors
1319

tests/ui/const-generics/mgca/type_const-mismatched-types.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
type const FREE: u32 = 5_usize;
55
//~^ ERROR the constant `5` is not of type `u32`
6-
//~| ERROR mismatched types
76

87
type const FREE2: isize = FREE;
98
//~^ ERROR the constant `5` is not of type `isize`
@@ -14,7 +13,7 @@ trait Tr {
1413

1514
impl Tr for () {
1615
type const N: usize = false;
17-
//~^ ERROR mismatched types
16+
//~^ ERROR the constant `false` is not of type `usize`
1817
}
1918

2019
fn main() {}

tests/ui/const-generics/mgca/type_const-mismatched-types.stderr

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,16 @@ LL | type const FREE: u32 = 5_usize;
55
| ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `usize`
66

77
error: the constant `5` is not of type `isize`
8-
--> $DIR/type_const-mismatched-types.rs:8:1
8+
--> $DIR/type_const-mismatched-types.rs:7:1
99
|
1010
LL | type const FREE2: isize = FREE;
1111
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
1212

13-
error[E0308]: mismatched types
14-
--> $DIR/type_const-mismatched-types.rs:16:27
13+
error: the constant `false` is not of type `usize`
14+
--> $DIR/type_const-mismatched-types.rs:15:5
1515
|
1616
LL | type const N: usize = false;
17-
| ^^^^^ expected `usize`, found `bool`
17+
| ^^^^^^^^^^^^^^^^^^^ expected `usize`, found `bool`
1818

19-
error[E0308]: mismatched types
20-
--> $DIR/type_const-mismatched-types.rs:4:24
21-
|
22-
LL | type const FREE: u32 = 5_usize;
23-
| ^^^^^^^ expected `u32`, found `usize`
24-
|
25-
help: change the type of the numeric literal from `usize` to `u32`
26-
|
27-
LL - type const FREE: u32 = 5_usize;
28-
LL + type const FREE: u32 = 5_u32;
29-
|
30-
31-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
3220

33-
For more information about this error, try `rustc --explain E0308`.

tests/ui/const-generics/mgca/type_const-only-in-impl-omitted-type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct GoodS;
1010
impl BadTr for GoodS {
1111
type const NUM: = 84;
1212
//~^ ERROR: missing type for `const` item
13+
//~| ERROR: type annotations needed for the literal
1314

1415
}
1516

tests/ui/const-generics/mgca/type_const-only-in-impl-omitted-type.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ LL | type const NUM: = 84;
55
| ^ help: provide a type for the associated constant: `usize`
66

77
error: use of trait associated const not defined as `type const`
8-
--> $DIR/type_const-only-in-impl-omitted-type.rs:16:43
8+
--> $DIR/type_const-only-in-impl-omitted-type.rs:17:43
99
|
1010
LL | fn accept_bad_tr<const N: usize, T: BadTr<NUM = { N }>>(_x: &T) {}
1111
| ^^^^^^^^^^^
1212
|
1313
= note: the declaration in the trait must begin with `type const` not just `const` alone
1414

15-
error: aborting due to 2 previous errors
15+
error: type annotations needed for the literal
16+
--> $DIR/type_const-only-in-impl-omitted-type.rs:11:23
17+
|
18+
LL | type const NUM: = 84;
19+
| ^^
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)