Skip to content

Commit e36ddcd

Browse files
committed
Auto merge of #162517 - khyperia:yeet-type-const, r=BoxyUwU
yeet `type const` tracking issue: #132980 zulip thread context (design decussion of future of gca): [#project-const-generics > talkies at last @ 💬](https://rust-lang.zulipchat.com/#narrow/channel/260443-project-const-generics/topic/talkies.20at.20last/near/620368825) this PR does two things: - implement `#[rustc_always_gca]` (bikeshed name thread: [#project-const-generics > #[always_gca] bikeshed](https://rust-lang.zulipchat.com/#narrow/channel/260443-project-const-generics/topic/.23.5Balways_gca.5D.20bikeshed/with/622249110) ) - remove `type const` syntax it also migrates the test suite to the syntax introduced in #162179 i.e. the new way to write `type const` is now `const ITEM: T = core::direct_const_arg!(V);` (ouch my hands, that took a while) r? @BoxyUwU
2 parents 8b82151 + f5eb9e4 commit e36ddcd

363 files changed

Lines changed: 1829 additions & 1964 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: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4043,17 +4043,9 @@ pub struct ConstItem {
40434043
pub generics: Generics,
40444044
pub ty: Box<Ty>,
40454045
pub body: Option<Box<Expr>>,
4046-
#[visitable(ignore)]
4047-
pub kind: ConstItemKind,
40484046
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
40494047
}
40504048

4051-
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq, Eq)]
4052-
pub enum ConstItemKind {
4053-
Body,
4054-
TypeConst,
4055-
}
4056-
40574049
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
40584050
pub struct ConstBlockItem {
40594051
pub id: NodeId,

‎compiler/rustc_ast_lowering/src/item.rs‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
273273
generics,
274274
ty,
275275
body,
276-
kind,
277276
define_opaque,
278277
}) => {
279278
let ident = self.lower_ident(*ident);
@@ -285,7 +284,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
285284
ty,
286285
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
287286
);
288-
let rhs = this.lower_const_item_rhs(body, *kind, span);
287+
let rhs = this.lower_const_item_rhs(body, span);
289288
(ty, rhs)
290289
},
291290
);
@@ -925,13 +924,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
925924

926925
let (ident, generics, kind, has_value) = match &i.kind {
927926
AssocItemKind::Const(ConstItem {
928-
ident,
929-
generics,
930-
ty,
931-
body,
932-
kind,
933-
define_opaque,
934-
..
927+
ident, generics, ty, body, define_opaque, ..
935928
}) => {
936929
let (generics, kind) = self.lower_generics(
937930
generics,
@@ -943,7 +936,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
943936
);
944937
// Trait associated consts don't need an expression/body.
945938
let rhs = if body.is_some() {
946-
Some(this.lower_const_item_rhs(body, *kind, i.span))
939+
Some(this.lower_const_item_rhs(body, i.span))
947940
} else {
948941
None
949942
};
@@ -1188,13 +1181,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11881181

11891182
let (ident, (generics, kind)) = match &i.kind {
11901183
AssocItemKind::Const(ConstItem {
1191-
ident,
1192-
generics,
1193-
ty,
1194-
body,
1195-
kind,
1196-
define_opaque,
1197-
..
1184+
ident, generics, ty, body, define_opaque, ..
11981185
}) => (
11991186
*ident,
12001187
self.lower_generics(
@@ -1206,7 +1193,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12061193
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
12071194
);
12081195
this.lower_define_opaque(hir_id, &define_opaque);
1209-
let rhs = this.lower_const_item_rhs(body, *kind, i.span);
1196+
let rhs = this.lower_const_item_rhs(body, i.span);
12101197
hir::ImplItemKind::Const(ty, rhs)
12111198
},
12121199
),

‎compiler/rustc_ast_lowering/src/lib.rs‎

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,56 +2682,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
26822682
fn lower_const_item_rhs(
26832683
&mut self,
26842684
body: &Option<Box<Expr>>,
2685-
kind: ConstItemKind,
26862685
span: Span,
26872686
) -> hir::ConstItemRhs<'hir> {
2688-
match (body, kind) {
2689-
(body, ConstItemKind::Body) => {
2690-
let is_direct = |body| {
2691-
if self.tcx.features().macroless_const_item_generic_const_args() {
2692-
self.can_lower_expr_to_const_arg_direct(
2693-
body,
2694-
DirectConstArgContext::MacrolessMinGenericConstArgs,
2695-
)
2696-
.is_ok()
2697-
} else {
2698-
// do not check can_lower_expr_to_const_arg_direct, but rather just
2699-
// ExprKind::DirectConstArg, because we don't want e.g.
2700-
// `impl<const N: u8> { const C: u8 = N; }` to be a direct-rhs const
2701-
matches!(body, Expr { kind: ExprKind::DirectConstArg(_), .. })
2702-
}
2703-
};
2704-
// N.B.: the feature gate for this is generic_const_args, not min_generic_const_args
2705-
if self.tcx.features().generic_const_args()
2706-
&& let Some(body) = body
2707-
&& is_direct(body)
2708-
{
2709-
hir::ConstItemRhs::Direct(
2710-
self.arena.alloc(self.lower_expr_to_const_arg_direct(&body, None)),
2711-
)
2712-
} else {
2713-
hir::ConstItemRhs::Body(self.lower_const_body(span, body.as_deref()))
2714-
}
2715-
}
2716-
(Some(body), ConstItemKind::TypeConst) => hir::ConstItemRhs::Direct(self.arena.alloc(
2717-
match self.can_lower_expr_to_const_arg_direct(
2718-
&body,
2687+
let is_direct = |body| {
2688+
if self.tcx.features().macroless_const_item_generic_const_args() {
2689+
self.can_lower_expr_to_const_arg_direct(
2690+
body,
27192691
DirectConstArgContext::MacrolessMinGenericConstArgs,
2720-
) {
2721-
Ok(()) => self.lower_expr_to_const_arg_direct(&body, None),
2722-
Err(err) => err.emit(self),
2723-
},
2724-
)),
2725-
(None, ConstItemKind::TypeConst) => {
2726-
let const_arg = ConstArg {
2727-
hir_id: self.next_id(),
2728-
kind: hir::ConstArgKind::Error(
2729-
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
2730-
),
2731-
span: DUMMY_SP,
2732-
};
2733-
hir::ConstItemRhs::Direct(self.arena.alloc(const_arg))
2692+
)
2693+
.is_ok()
2694+
} else {
2695+
// do not check can_lower_expr_to_const_arg_direct, but rather just
2696+
// ExprKind::DirectConstArg, because we don't want e.g.
2697+
// `impl<const N: u8> { const C: u8 = N; }` to be a direct-rhs const
2698+
matches!(body, Expr { kind: ExprKind::DirectConstArg(_), .. })
27342699
}
2700+
};
2701+
if self.tcx.features().min_generic_const_args()
2702+
&& let Some(body) = body
2703+
&& is_direct(body)
2704+
{
2705+
hir::ConstItemRhs::Direct(
2706+
self.arena.alloc(self.lower_expr_to_const_arg_direct(&body, None)),
2707+
)
2708+
} else {
2709+
hir::ConstItemRhs::Body(self.lower_const_body(span, body.as_deref()))
27352710
}
27362711
}
27372712

‎compiler/rustc_ast_lowering/src/path.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
113113
}
114114
// `a::b::Trait(Args)::TraitItem`
115115
Res::Def(DefKind::AssocFn, _)
116-
| Res::Def(DefKind::AssocConst { .. }, _)
116+
| Res::Def(DefKind::AssocConst, _)
117117
| Res::Def(DefKind::AssocTy, _)
118118
if i + 2 == proj_start =>
119119
{

‎compiler/rustc_ast_passes/src/feature_gate.rs‎

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
190190
ast::ItemKind::TyAlias(ast::TyAlias { ty: Some(ty), .. }) => {
191191
self.check_impl_trait(ty, false)
192192
}
193-
ast::ItemKind::Const(ast::ConstItem {
194-
kind: ast::ConstItemKind::TypeConst, ..
195-
}) => {
196-
// Make sure this is only allowed if the feature gate is enabled.
197-
// #![feature(min_generic_const_args)]
198-
gate!(self, min_generic_const_args, i.span, "top-level `type const` are unstable");
199-
}
200193

201194
_ => {}
202195
}
@@ -364,18 +357,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
364357
}
365358
false
366359
}
367-
ast::AssocItemKind::Const(ast::ConstItem {
368-
body,
369-
kind: ast::ConstItemKind::TypeConst,
370-
..
371-
}) => {
372-
// Make sure this is only allowed if the feature gate is enabled.
373-
// #![feature(min_generic_const_args)]
374-
gate!(self, min_generic_const_args, i.span, "associated `type const` are unstable");
375-
// Make sure associated `type const` defaults in traits are only allowed
376-
// if the feature gate is enabled.
377-
// #![feature(associated_type_defaults)]
378-
if ctxt == AssocCtxt::Trait && body.is_some() {
360+
ast::AssocItemKind::Const(ast::ConstItem { body: Some(_), .. }) => {
361+
if ctxt == AssocCtxt::Trait && attr::contains_name(&i.attrs, sym::rustc_always_gca)
362+
{
379363
gate!(
380364
self,
381365
associated_type_defaults,
@@ -511,25 +495,6 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
511495
gate!(visitor, min_generic_const_args, span, "associated const equality is incomplete");
512496
}
513497

514-
// `mgca_type_const_syntax` is part of `min_generic_const_args` so if
515-
// either or both are enabled we don't need to emit a feature error.
516-
for &span in spans.get(&sym::mgca_type_const_syntax).into_flat_iter() {
517-
if visitor.features.min_generic_const_args()
518-
|| visitor.features.mgca_type_const_syntax()
519-
|| span.allows_unstable(sym::min_generic_const_args)
520-
|| span.allows_unstable(sym::mgca_type_const_syntax)
521-
{
522-
continue;
523-
}
524-
feature_err(
525-
visitor.sess,
526-
sym::min_generic_const_args,
527-
span,
528-
"`type const` syntax is experimental",
529-
)
530-
.emit();
531-
}
532-
533498
// Negative bounds are *super* internal. We require `-Zinternal-testing-features` *and*
534499
// `#![feature(negative_bounds)]` to prevent proliferation. Under no circumstances do we
535500
// want to advertise the flag and the feature name to users!

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ impl<'a> State<'a> {
229229
generics,
230230
ty,
231231
body,
232-
kind: _,
233232
define_opaque,
234233
}) => {
235234
self.print_item_const(
@@ -621,7 +620,6 @@ impl<'a> State<'a> {
621620
generics,
622621
ty,
623622
body,
624-
kind: _,
625623
define_opaque,
626624
}) => {
627625
self.print_item_const(

‎compiler/rustc_attr_ir/src/data_structures.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ pub enum AttributeKind {
785785
/// Represents `#[allow_internal_unstable]`.
786786
AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),
787787

788+
/// Represents `#[rustc_always_gca]`
789+
AlwaysGca,
790+
788791
/// Represents `#[automatically_derived]`
789792
AutomaticallyDerived,
790793

‎compiler/rustc_attr_ir/src/encode_cross_crate.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl AttributeKind {
2020
// tidy-alphabetical-start
2121
AllowInternalUnsafe(..) => Yes,
2222
AllowInternalUnstable(..) => Yes,
23+
AlwaysGca => Yes,
2324
AutomaticallyDerived => Yes,
2425
CfgAttrTrace(..) => Yes,
2526
CfgTrace(..) => Yes,

‎compiler/rustc_attr_parsing/src/attributes/semantics.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ impl NoArgsAttributeParser for ComptimeParser {
2727
const STABILITY: AttributeStability = unstable!(rustc_attrs);
2828
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcComptime;
2929
}
30+
31+
pub(crate) struct AlwaysGcaParser;
32+
impl NoArgsAttributeParser for AlwaysGcaParser {
33+
const PATH: &[Symbol] = &[sym::rustc_always_gca];
34+
const ALLOWED_TARGETS: AllowedTargets<'_> =
35+
AllowedTargets::AllowList(&[Allow(Target::AssocConst(AssocCtxt::Trait))]);
36+
const STABILITY: AttributeStability = unstable!(min_generic_const_args);
37+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::AlwaysGca;
38+
}

‎compiler/rustc_attr_parsing/src/context.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ attribute_parsers!(
259259
Single<UnrollParser>,
260260
Single<WindowsSubsystemParser>,
261261
Single<WithoutArgs<AllowInternalUnsafeParser>>,
262+
Single<WithoutArgs<AlwaysGcaParser>>,
262263
Single<WithoutArgs<AutomaticallyDerivedParser>>,
263264
Single<WithoutArgs<ColdParser>>,
264265
Single<WithoutArgs<CompilerBuiltinsParser>>,

0 commit comments

Comments
 (0)