Skip to content

Commit 63fd1cd

Browse files
Rollup merge of rust-lang#153075 - reddevilmidzy:mgca-neg, r=BoxyUwU
mGCA: Lower negated literals directly and reject non-integer negations follow up rust-lang#152001 resolve: rust-lang#152246 r? BoxyUwU
2 parents 6143c77 + d2619b5 commit 63fd1cd

18 files changed

Lines changed: 95 additions & 122 deletions

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,16 +2521,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25212521
ExprKind::Block(block, _) => {
25222522
if let [stmt] = block.stmts.as_slice()
25232523
&& let StmtKind::Expr(expr) = &stmt.kind
2524-
&& matches!(
2525-
expr.kind,
2526-
ExprKind::Block(..)
2527-
| ExprKind::Path(..)
2528-
| ExprKind::Struct(..)
2529-
| ExprKind::Call(..)
2530-
| ExprKind::Tup(..)
2531-
| ExprKind::Array(..)
2532-
| ExprKind::ConstBlock(..)
2533-
)
25342524
{
25352525
return self.lower_expr_to_const_arg_direct(expr);
25362526
}
@@ -2553,6 +2543,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25532543
let span = expr.span;
25542544
let literal = self.lower_lit(literal, span);
25552545

2546+
if !matches!(literal.node, LitKind::Int(..)) {
2547+
let err =
2548+
self.dcx().struct_span_err(expr.span, "negated literal must be an integer");
2549+
2550+
return ConstArg {
2551+
hir_id: self.next_id(),
2552+
kind: hir::ConstArgKind::Error(err.emit()),
2553+
span,
2554+
};
2555+
}
2556+
25562557
ConstArg {
25572558
hir_id: self.lower_node_id(expr.id),
25582559
kind: hir::ConstArgKind::Literal { lit: literal.node, negated: true },

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,8 +1628,7 @@ impl<'a> Parser<'a> {
16281628
let first_expr = self.parse_expr()?;
16291629
if self.eat(exp!(Semi)) {
16301630
// Repeating array syntax: `[ 0; 512 ]`
1631-
let count =
1632-
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?;
1631+
let count = self.parse_expr_anon_const(|_, _| MgcaDisambiguation::Direct)?;
16331632
self.expect(close)?;
16341633
ExprKind::Repeat(first_expr, count)
16351634
} else if self.eat(exp!(Comma)) {

compiler/rustc_parse/src/parser/item.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,9 +1587,7 @@ impl<'a> Parser<'a> {
15871587

15881588
let rhs = match (self.eat(exp!(Eq)), const_arg) {
15891589
(true, true) => ConstItemRhsKind::TypeConst {
1590-
rhs: Some(
1591-
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?,
1592-
),
1590+
rhs: Some(self.parse_expr_anon_const(|_, _| MgcaDisambiguation::Direct)?),
15931591
},
15941592
(true, false) => ConstItemRhsKind::Body { rhs: Some(self.parse_expr()?) },
15951593
(false, true) => ConstItemRhsKind::TypeConst { rhs: None },

compiler/rustc_parse/src/parser/path.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -915,28 +915,7 @@ impl<'a> Parser<'a> {
915915
});
916916
}
917917

918-
let mgca_disambiguation = self.mgca_direct_lit_hack(&expr);
919-
Ok((expr, mgca_disambiguation))
920-
}
921-
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-
}
918+
Ok((expr, MgcaDisambiguation::Direct))
940919
}
941920

942921
/// Parse a generic argument in a path segment.

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,7 @@ impl<'a> Parser<'a> {
658658
};
659659

660660
let ty = if self.eat(exp!(Semi)) {
661-
let mut length =
662-
self.parse_expr_anon_const(|this, expr| this.mgca_direct_lit_hack(expr))?;
661+
let mut length = self.parse_expr_anon_const(|_, _| MgcaDisambiguation::Direct)?;
663662

664663
if let Err(e) = self.expect(exp!(CloseBracket)) {
665664
// Try to recover from `X<Y, ...>` when `X::<Y, ...>` works
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-complex.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ LL | takes_array::<{ [1, 2, 1 + 2] }>();
55
| ^^^^^
66

77
error: complex const arguments must be placed inside of a `const` block
8-
--> $DIR/array-expr-complex.rs:10:19
8+
--> $DIR/array-expr-complex.rs:10:21
99
|
1010
LL | takes_array::<{ [X; 3] }>();
11-
| ^^^^^^^^^^
11+
| ^^^^^^
1212

1313
error: complex const arguments must be placed inside of a `const` block
14-
--> $DIR/array-expr-complex.rs:12:19
14+
--> $DIR/array-expr-complex.rs:12:21
1515
|
1616
LL | takes_array::<{ [0; Y] }>();
17-
| ^^^^^^^^^^
17+
| ^^^^^^
1818

1919
error: aborting due to 3 previous errors
2020

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/explicit_anon_consts.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: complex const arguments must be placed inside of a `const` block
2-
--> $DIR/explicit_anon_consts.rs:13:33
2+
--> $DIR/explicit_anon_consts.rs:13:35
33
|
44
LL | type Adt4<const N: usize> = Foo<{ 1 + 1 }>;
5-
| ^^^^^^^^^
5+
| ^^^^^
66

77
error: complex const arguments must be placed inside of a `const` block
88
--> $DIR/explicit_anon_consts.rs:21:34
@@ -17,22 +17,22 @@ LL | let _4 = [(); 1 + 1];
1717
| ^^^^^
1818

1919
error: complex const arguments must be placed inside of a `const` block
20-
--> $DIR/explicit_anon_consts.rs:45:43
20+
--> $DIR/explicit_anon_consts.rs:45:45
2121
|
2222
LL | type const ITEM4<const N: usize>: usize = { 1 + 1 };
23-
| ^^^^^^^^^
23+
| ^^^^^
2424

2525
error: complex const arguments must be placed inside of a `const` block
26-
--> $DIR/explicit_anon_consts.rs:62:23
26+
--> $DIR/explicit_anon_consts.rs:62:25
2727
|
2828
LL | T4: Trait<ASSOC = { 1 + 1 }>,
29-
| ^^^^^^^^^
29+
| ^^^^^
3030

3131
error: complex const arguments must be placed inside of a `const` block
32-
--> $DIR/explicit_anon_consts.rs:71:50
32+
--> $DIR/explicit_anon_consts.rs:71:52
3333
|
3434
LL | struct Default4<const N: usize, const M: usize = { 1 + 1 }>;
35-
| ^^^^^^^^^
35+
| ^^^^^
3636

3737
error: generic parameters may not be used in const operations
3838
--> $DIR/explicit_anon_consts.rs:42:51

0 commit comments

Comments
 (0)