Skip to content

Commit 73a991f

Browse files
committed
Allow provisional mgca syntax of type const <IDENT> = <EXPR> to be reconized.
Revert, but without type const. Update symbol for feature err, then update suggestion output, and lastly update tests that change because of those. Update these new tests with the correct syntax, and few existing tests with the new outputs the merge with main added. Fix for tidyfmt and some errors when manually resolving a merge conflicts. Update these tests to use update error messages and type const syntax. Update comments and error message to use new syntax instead of old type_const attribute. Remove the type_const attribute update some more tests to use the new syntax. Update these test cases. update feature gate test Change gate logic for `mgca_type_const_syntax` to work also if `min_generic_const_args` is enabled. Create a new feature gate that checks for the feature before expansion. Make rustfmt handle the `type const` syntax correctly. Add a convience method to check if a RhsKind is type const. Rename `Const` discriminant to `Body` for `ConstItemRhsKind` Give the `TraitItemKind` flag an enum instead of a simple bool to better describe what the flag is for. Update formatting for these match statements. Update clippy test to use type const syntax. Update test to use type const syntax. update rustfmt to match ast items. Update clippy to match ast and hir items. Few more test cases that used old attribute, instead of 'type const' Update to match the output from the feature gate checks. tidyfmt adjustments. Update the is_type_const, so I can constrain record!(..) in encoder.rs Update conditional compilation test. Move the feature gate to after expansion to allow for cfg(...) to work. Update some more tests to use the new syntax. Update type const tests in associated-const-bindings to use new syntax. Don't check based off the attribute, but the item here. Update some tests outside of the const_generics folder that were using #[type_const] update the tests in associated consts that use #[type_const] to use type const Update these mgca tests with the type const syntax. Add a flag to TraitItemKind for detecting type const for now. Maybe later change ItemConstRhs to have optional consts but that touches a lot more lines of code. Don't need into for these now that it's a query. Add is_type_const query to handle foreign def ids. update this test to use type const syntax. Fix logic here, we only want to lower if there is expression in this case. Update built-in macros to use ConstItemRhsKind Update more instance of the old ConstItemRhs. Rename ConstItemKind to ConstItemRhsKind, I noticed there is a typed called ConstantItemKind, so add the Rhs to the name to avoid confusion. Update lower to use ConstItemKind Add an other helper method to check if the rhs kinda has an expr. Update item parse to use ConstItemKind enum. Felt the field name could a be little clear when editing a few other things. Change the ConstItem struct see know if we have a type const or regular const. Make sure this syntax is properly feature gated.
1 parent 4cd4c18 commit 73a991f

204 files changed

Lines changed: 1014 additions & 906 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: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3869,27 +3869,44 @@ pub struct ConstItem {
38693869
pub ident: Ident,
38703870
pub generics: Generics,
38713871
pub ty: Box<Ty>,
3872-
pub rhs: Option<ConstItemRhs>,
3872+
pub rhs_kind: ConstItemRhsKind,
38733873
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
38743874
}
38753875

38763876
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3877-
pub enum ConstItemRhs {
3878-
TypeConst(AnonConst),
3879-
Body(Box<Expr>),
3877+
pub enum ConstItemRhsKind {
3878+
Body { rhs: Option<Box<Expr>> },
3879+
TypeConst { rhs: Option<AnonConst> },
38803880
}
38813881

3882-
impl ConstItemRhs {
3883-
pub fn span(&self) -> Span {
3884-
self.expr().span
3882+
impl ConstItemRhsKind {
3883+
pub fn new_body(rhs: Box<Expr>) -> Self {
3884+
Self::Body { rhs: Some(rhs) }
3885+
}
3886+
3887+
pub fn span(&self) -> Option<Span> {
3888+
Some(self.expr()?.span)
3889+
}
3890+
3891+
pub fn expr(&self) -> Option<&Expr> {
3892+
match self {
3893+
Self::Body { rhs: Some(body) } => Some(&body),
3894+
Self::TypeConst { rhs: Some(anon) } => Some(&anon.value),
3895+
_ => None,
3896+
}
38853897
}
38863898

3887-
pub fn expr(&self) -> &Expr {
3899+
pub fn has_expr(&self) -> bool {
38883900
match self {
3889-
ConstItemRhs::TypeConst(anon_const) => &anon_const.value,
3890-
ConstItemRhs::Body(expr) => expr,
3901+
Self::Body { rhs: Some(_) } => true,
3902+
Self::TypeConst { rhs: Some(_) } => true,
3903+
_ => false,
38913904
}
38923905
}
3906+
3907+
pub fn is_type_const(&self) -> bool {
3908+
matches!(self, &Self::TypeConst { .. })
3909+
}
38933910
}
38943911

38953912
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ macro_rules! common_visitor_and_walkers {
427427
Const,
428428
ConstBlockItem,
429429
ConstItem,
430-
ConstItemRhs,
430+
ConstItemRhsKind,
431431
Defaultness,
432432
Delegation,
433433
DelegationMac,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
288288
ident,
289289
generics,
290290
ty,
291-
rhs,
291+
rhs_kind,
292292
define_opaque,
293293
}) => {
294294
let ident = self.lower_ident(*ident);
@@ -301,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
301301
ty,
302302
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
303303
);
304-
let rhs = this.lower_const_item_rhs(attrs, rhs.as_ref(), span);
304+
let rhs = this.lower_const_item_rhs(rhs_kind, span);
305305
(ty, rhs)
306306
},
307307
);
@@ -941,7 +941,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
941941

942942
let (ident, generics, kind, has_default) = match &i.kind {
943943
AssocItemKind::Const(box ConstItem {
944-
ident, generics, ty, rhs, define_opaque, ..
944+
ident,
945+
generics,
946+
ty,
947+
rhs_kind,
948+
define_opaque,
949+
..
945950
}) => {
946951
let (generics, kind) = self.lower_generics(
947952
generics,
@@ -952,15 +957,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
952957
ty,
953958
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
954959
);
955-
let rhs = rhs
956-
.as_ref()
957-
.map(|rhs| this.lower_const_item_rhs(attrs, Some(rhs), i.span));
958-
hir::TraitItemKind::Const(ty, rhs)
960+
// Trait associated consts don't need an expression/body.
961+
let rhs = if rhs_kind.has_expr() {
962+
Some(this.lower_const_item_rhs(rhs_kind, i.span))
963+
} else {
964+
None
965+
};
966+
hir::TraitItemKind::Const(ty, rhs, rhs_kind.is_type_const().into())
959967
},
960968
);
961969

962970
if define_opaque.is_some() {
963-
if rhs.is_some() {
971+
if rhs_kind.has_expr() {
964972
self.lower_define_opaque(hir_id, &define_opaque);
965973
} else {
966974
self.dcx().span_err(
@@ -970,7 +978,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
970978
}
971979
}
972980

973-
(*ident, generics, kind, rhs.is_some())
981+
(*ident, generics, kind, rhs_kind.has_expr())
974982
}
975983
AssocItemKind::Fn(box Fn {
976984
sig, ident, generics, body: None, define_opaque, ..
@@ -1154,7 +1162,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
11541162

11551163
let (ident, (generics, kind)) = match &i.kind {
11561164
AssocItemKind::Const(box ConstItem {
1157-
ident, generics, ty, rhs, define_opaque, ..
1165+
ident,
1166+
generics,
1167+
ty,
1168+
rhs_kind,
1169+
define_opaque,
1170+
..
11581171
}) => (
11591172
*ident,
11601173
self.lower_generics(
@@ -1167,7 +1180,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11671180
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
11681181
);
11691182
this.lower_define_opaque(hir_id, &define_opaque);
1170-
let rhs = this.lower_const_item_rhs(attrs, rhs.as_ref(), i.span);
1183+
let rhs = this.lower_const_item_rhs(rhs_kind, i.span);
11711184
hir::ImplItemKind::Const(ty, rhs)
11721185
},
11731186
),

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,15 +2374,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23742374

23752375
fn lower_const_item_rhs(
23762376
&mut self,
2377-
attrs: &[hir::Attribute],
2378-
rhs: Option<&ConstItemRhs>,
2377+
rhs_kind: &ConstItemRhsKind,
23792378
span: Span,
23802379
) -> hir::ConstItemRhs<'hir> {
2381-
match rhs {
2382-
Some(ConstItemRhs::TypeConst(anon)) => {
2380+
match rhs_kind {
2381+
ConstItemRhsKind::Body { rhs: Some(body) } => {
2382+
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
2383+
}
2384+
ConstItemRhsKind::Body { rhs: None } => {
2385+
hir::ConstItemRhs::Body(self.lower_const_body(span, None))
2386+
}
2387+
ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
23832388
hir::ConstItemRhs::TypeConst(self.lower_anon_const_to_const_arg_and_alloc(anon))
23842389
}
2385-
None if find_attr!(attrs, AttributeKind::TypeConst(_)) => {
2390+
ConstItemRhsKind::TypeConst { rhs: None } => {
23862391
let const_arg = ConstArg {
23872392
hir_id: self.next_id(),
23882393
kind: hir::ConstArgKind::Error(
@@ -2392,10 +2397,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23922397
};
23932398
hir::ConstItemRhs::TypeConst(self.arena.alloc(const_arg))
23942399
}
2395-
Some(ConstItemRhs::Body(body)) => {
2396-
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
2397-
}
2398-
None => hir::ConstItemRhs::Body(self.lower_const_body(span, None)),
23992400
}
24002401
}
24012402

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,9 +1361,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13611361
}
13621362
});
13631363
}
1364-
ItemKind::Const(box ConstItem { defaultness, ident, rhs, .. }) => {
1364+
ItemKind::Const(box ConstItem { defaultness, ident, rhs_kind, .. }) => {
13651365
self.check_defaultness(item.span, *defaultness);
1366-
if rhs.is_none() {
1366+
if !rhs_kind.has_expr() {
13671367
self.dcx().emit_err(errors::ConstWithoutBody {
13681368
span: item.span,
13691369
replace_span: self.ending_semi_or_hi(item.span),
@@ -1715,11 +1715,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
17151715

17161716
if let AssocCtxt::Impl { .. } = ctxt {
17171717
match &item.kind {
1718-
AssocItemKind::Const(box ConstItem { rhs: None, .. }) => {
1719-
self.dcx().emit_err(errors::AssocConstWithoutBody {
1720-
span: item.span,
1721-
replace_span: self.ending_semi_or_hi(item.span),
1722-
});
1718+
AssocItemKind::Const(box ConstItem { rhs_kind, .. }) => {
1719+
if !rhs_kind.has_expr() {
1720+
self.dcx().emit_err(errors::AssocConstWithoutBody {
1721+
span: item.span,
1722+
replace_span: self.ending_semi_or_hi(item.span),
1723+
});
1724+
}
17231725
}
17241726
AssocItemKind::Fn(box Fn { body, .. }) => {
17251727
if body.is_none() && !self.is_sdylib_interface {

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,14 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
249249
ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => {
250250
self.check_impl_trait(ty, false)
251251
}
252+
ast::ItemKind::Const(box ast::ConstItem {
253+
rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
254+
..
255+
}) => {
256+
// Make sure this is only allowed if the feature gate is enabled.
257+
// #![feature(min_generic_const_args)]
258+
gate!(&self, min_generic_const_args, i.span, "top-level `type const` are unstable");
259+
}
252260

253261
_ => {}
254262
}
@@ -422,6 +430,20 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
422430
}
423431
false
424432
}
433+
ast::AssocItemKind::Const(box ast::ConstItem {
434+
rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
435+
..
436+
}) => {
437+
// Make sure this is only allowed if the feature gate is enabled.
438+
// #![feature(min_generic_const_args)]
439+
gate!(
440+
&self,
441+
min_generic_const_args,
442+
i.span,
443+
"associated `type const` are unstable"
444+
);
445+
false
446+
}
425447
_ => false,
426448
};
427449
if let ast::Defaultness::Default(_) = i.kind.defaultness() {
@@ -528,6 +550,27 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
528550
}
529551
}
530552
}
553+
// `mgca_type_const_syntax` is part of `min_generic_const_args` so either
554+
// or both are enabled we don't need to emit a feature error.
555+
if let Some(spans) = spans.get(&sym::mgca_type_const_syntax) {
556+
for span in spans {
557+
if visitor.features.min_generic_const_args()
558+
|| visitor.features.mgca_type_const_syntax()
559+
|| span.allows_unstable(sym::min_generic_const_args)
560+
|| span.allows_unstable(sym::mgca_type_const_syntax)
561+
{
562+
continue;
563+
}
564+
feature_err(
565+
&visitor.sess,
566+
sym::min_generic_const_args,
567+
*span,
568+
"`type const` syntax is experimental",
569+
)
570+
.emit();
571+
}
572+
}
573+
531574
gate_all!(global_registration, "global registration is experimental");
532575
gate_all!(return_type_notation, "return type notation is experimental");
533576
gate_all!(pin_ergonomics, "pinned reference syntax is experimental");

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ impl<'a> State<'a> {
221221
ident,
222222
generics,
223223
ty,
224-
rhs,
224+
rhs_kind,
225225
define_opaque,
226226
}) => {
227227
self.print_item_const(
228228
*ident,
229229
None,
230230
generics,
231231
ty,
232-
rhs.as_ref().map(|ct| ct.expr()),
232+
rhs_kind.expr(),
233233
&item.vis,
234234
ast::Safety::Default,
235235
*defaultness,
@@ -573,15 +573,15 @@ impl<'a> State<'a> {
573573
ident,
574574
generics,
575575
ty,
576-
rhs,
576+
rhs_kind,
577577
define_opaque,
578578
}) => {
579579
self.print_item_const(
580580
*ident,
581581
None,
582582
generics,
583583
ty,
584-
rhs.as_ref().map(|ct| ct.expr()),
584+
rhs_kind.expr(),
585585
vis,
586586
ast::Safety::Default,
587587
*defaultness,

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,6 @@ impl<S: Stage> NoArgsAttributeParser<S> for ParenSugarParser {
6666
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcParenSugar;
6767
}
6868

69-
pub(crate) struct TypeConstParser;
70-
impl<S: Stage> NoArgsAttributeParser<S> for TypeConstParser {
71-
const PATH: &[Symbol] = &[sym::type_const];
72-
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
73-
const ALLOWED_TARGETS: AllowedTargets =
74-
AllowedTargets::AllowList(&[Allow(Target::Const), Allow(Target::AssocConst)]);
75-
const CREATE: fn(Span) -> AttributeKind = AttributeKind::TypeConst;
76-
}
77-
7869
// Markers
7970

8071
pub(crate) struct MarkerParser;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ attribute_parsers!(
297297
Single<WithoutArgs<StdInternalSymbolParser>>,
298298
Single<WithoutArgs<ThreadLocalParser>>,
299299
Single<WithoutArgs<TrackCallerParser>>,
300-
Single<WithoutArgs<TypeConstParser>>,
301300
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,
302301
// tidy-alphabetical-end
303302
];

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn expand(
4343

4444
// Generate anonymous constant serving as container for the allocator methods.
4545
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
46-
let const_body = ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts)));
46+
let const_body = ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts)));
4747
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
4848
let const_item = if is_stmt {
4949
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))

0 commit comments

Comments
 (0)