Skip to content

Commit 3dc22a3

Browse files
authored
Rollup merge of #158617 - khyperia:gca-syntax-flip, r=BoxyUwU
allow mGCA const arguments to fall back to anon consts makes good progress on rust-lang/project-const-generics#108 `min_generic_const_args` (mGCA) is all about "is this const lowered to a const item with a body, or, is it "directly" represented and visible in the type system?" Here's various answers to the question of "can we represent this const arg directly?" - if we answer "no", then we fall back to an anon const (or error, sometimes, depending on context) on **stable**, the full list of things can be directly represented is (... it's just the one thing) - paths to generic const parameters on `main`, under mGCA, **before** this PR: - absolutely everything, if something cannot be represented directly, compiler error - `const { }` gives you an escape hatch to go back to being an anon const on macro**ful** gca, the directly represented things will be: - paths to const parameters - `direct_const_arg!` macro on macro**less** gca, the directly represented things will be: - paths to const parameters - `direct_const_arg!` macro (not *particularly* useful under macro**less** gca) - struct expressions, arrays, blah blah - currently, paths to *anything*, even if that produces a compiler error later down the line (potential followup work here...) this PR implements macro**less** gca, with the intent of a quick follow-up introducing a "macroless gca" feature and transitioning `#[feature(min_generic_const_args)]` to be macro**ful** gca Also of note is that this PR removes the logic to skip generating "fake" DefIds for directly represented AnonConsts under mGCA, we now always generate a DefId when encountering an AnonConst. This lines up with stable, which has a fake AnonConst DefId for a directly represented plain path generic parameter (i.e. the bullet point under the "stable" entry above). r? @BoxyUwU
2 parents 9e7a9ba + 67986af commit 3dc22a3

73 files changed

Lines changed: 672 additions & 359 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_ast/src/ast.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,15 +1378,6 @@ pub enum UnsafeSource {
13781378
UserProvided,
13791379
}
13801380

1381-
/// Track whether under `feature(min_generic_const_args)` this anon const
1382-
/// was explicitly disambiguated as an anon const or not through the use of
1383-
/// `const { ... }` syntax.
1384-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, Copy, Walkable)]
1385-
pub enum MgcaDisambiguation {
1386-
AnonConst,
1387-
Direct,
1388-
}
1389-
13901381
/// A constant (expression) that's not an item or associated item,
13911382
/// but needs its own `DefId` for type-checking, const-eval, etc.
13921383
/// These are usually found nested inside types (e.g., array lengths)
@@ -1396,7 +1387,6 @@ pub enum MgcaDisambiguation {
13961387
pub struct AnonConst {
13971388
pub id: NodeId,
13981389
pub value: Box<Expr>,
1399-
pub mgca_disambiguation: MgcaDisambiguation,
14001390
}
14011391

14021392
/// An expression.
@@ -1627,6 +1617,7 @@ impl Expr {
16271617
| ExprKind::UnsafeBinderCast(..)
16281618
| ExprKind::While(..)
16291619
| ExprKind::Yield(YieldKind::Postfix(..))
1620+
| ExprKind::DirectConstArg(..)
16301621
| ExprKind::Err(_)
16311622
| ExprKind::Dummy => prefix_attrs_precedence(&self.attrs),
16321623
}
@@ -1920,6 +1911,9 @@ pub enum ExprKind {
19201911

19211912
UnsafeBinderCast(UnsafeBinderCastKind, Box<Expr>, Option<Box<Ty>>),
19221913

1914+
/// An mGCA `direct_const_arg!()` expression.
1915+
DirectConstArg(Box<Expr>),
1916+
19231917
/// Placeholder for an expression that wasn't syntactically well formed in some way.
19241918
Err(ErrorGuaranteed),
19251919

@@ -2566,6 +2560,8 @@ pub enum TyKind {
25662560
FieldOf(Box<Ty>, Option<Ident>, Ident),
25672561
/// A view of a type. `T.{ field_1, field_2 }`.
25682562
View(Box<Ty>, #[visitable(ignore)] ThinVec<Ident>),
2563+
/// An mGCA `direct_const_arg!()` expression.
2564+
DirectConstArg(Box<Expr>),
25692565
/// Sometimes we need a dummy value when no error has occurred.
25702566
Dummy,
25712567
/// Placeholder for a kind that has failed to be defined.

compiler/rustc_ast/src/util/classify.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub fn leading_labeled_expr(mut expr: &ast::Expr) -> bool {
155155
| Yeet(..)
156156
| Yield(..)
157157
| UnsafeBinderCast(..)
158+
| DirectConstArg(..)
158159
| Err(..)
159160
| Dummy => return false,
160161
}
@@ -240,6 +241,7 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<TrailingBrace<'_>> {
240241
| Try(_)
241242
| Yeet(None)
242243
| UnsafeBinderCast(..)
244+
| DirectConstArg(..)
243245
| Err(_)
244246
| Dummy => {
245247
break None;
@@ -301,9 +303,10 @@ fn type_trailing_braced_mac_call(mut ty: &ast::Ty) -> Option<&ast::MacCall> {
301303
| ast::TyKind::CVarArgs
302304
| ast::TyKind::Pat(..)
303305
| ast::TyKind::FieldOf(..)
306+
| ast::TyKind::View(..)
307+
| ast::TyKind::DirectConstArg(..)
304308
| ast::TyKind::Dummy
305-
| ast::TyKind::Err(..)
306-
| ast::TyKind::View(..) => break None,
309+
| ast::TyKind::Err(..) => break None,
307310
}
308311
}
309312
}

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ macro_rules! common_visitor_and_walkers {
418418
UnsafeBinderCastKind,
419419
BinOpKind,
420420
BlockCheckMode,
421-
MgcaDisambiguation,
422421
BorrowKind,
423422
BoundAsyncness,
424423
BoundConstness,
@@ -1074,6 +1073,8 @@ macro_rules! common_visitor_and_walkers {
10741073
visit_visitable!($($mut)? vis, bytes),
10751074
ExprKind::UnsafeBinderCast(kind, expr, ty) =>
10761075
visit_visitable!($($mut)? vis, kind, expr, ty),
1076+
ExprKind::DirectConstArg(expr) =>
1077+
visit_visitable!($($mut)? vis, expr),
10771078
ExprKind::Err(_guar) => {}
10781079
ExprKind::Dummy => {}
10791080
}

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
498498
}
499499

500500
ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
501+
502+
ExprKind::DirectConstArg(_) => {
503+
let e = self
504+
.tcx
505+
.dcx()
506+
.struct_span_err(
507+
e.span,
508+
"expected expression, found `direct_const_arg!()` constant",
509+
)
510+
.emit();
511+
hir::ExprKind::Err(e)
512+
}
501513
};
502514

503515
hir::Expr { hir_id: expr_hir_id, kind, span }
@@ -599,11 +611,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
599611
arg
600612
};
601613

602-
let anon_const = AnonConst {
603-
id: node_id,
604-
value: const_value,
605-
mgca_disambiguation: MgcaDisambiguation::AnonConst,
606-
};
614+
let anon_const = AnonConst { id: node_id, value: const_value };
607615
generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const)));
608616
} else {
609617
real_args.push(arg);

0 commit comments

Comments
 (0)