Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 0 additions & 8 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4035,17 +4035,9 @@ pub struct ConstItem {
pub generics: Generics,
pub ty: Box<Ty>,
pub body: Option<Box<Expr>>,
#[visitable(ignore)]
pub kind: ConstItemKind,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
}

#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq, Eq)]
pub enum ConstItemKind {
Body,
TypeConst,
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub struct ConstBlockItem {
pub id: NodeId,
Expand Down
23 changes: 5 additions & 18 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
generics,
ty,
body,
kind,
define_opaque,
}) => {
let ident = self.lower_ident(*ident);
Expand All @@ -284,7 +283,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
let rhs = this.lower_const_item_rhs(body, *kind, span);
let rhs = this.lower_const_item_rhs(body, span);
(ty, rhs)
},
);
Expand Down Expand Up @@ -924,13 +923,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

let (ident, generics, kind, has_value) = match &i.kind {
AssocItemKind::Const(ConstItem {
ident,
generics,
ty,
body,
kind,
define_opaque,
..
ident, generics, ty, body, define_opaque, ..
}) => {
let (generics, kind) = self.lower_generics(
generics,
Expand All @@ -942,7 +935,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
// Trait associated consts don't need an expression/body.
let rhs = if body.is_some() {
Some(this.lower_const_item_rhs(body, *kind, i.span))
Some(this.lower_const_item_rhs(body, i.span))
} else {
None
};
Expand Down Expand Up @@ -1187,13 +1180,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

let (ident, (generics, kind)) = match &i.kind {
AssocItemKind::Const(ConstItem {
ident,
generics,
ty,
body,
kind,
define_opaque,
..
ident, generics, ty, body, define_opaque, ..
}) => (
*ident,
self.lower_generics(
Expand All @@ -1205,7 +1192,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
this.lower_define_opaque(hir_id, &define_opaque);
let rhs = this.lower_const_item_rhs(body, *kind, i.span);
let rhs = this.lower_const_item_rhs(body, i.span);
hir::ImplItemKind::Const(ty, rhs)
},
),
Expand Down
67 changes: 21 additions & 46 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2671,56 +2671,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_const_item_rhs(
&mut self,
body: &Option<Box<Expr>>,
kind: ConstItemKind,
span: Span,
) -> hir::ConstItemRhs<'hir> {
match (body, kind) {
(body, ConstItemKind::Body) => {
let is_direct = |body| {
if self.tcx.features().macroless_generic_const_args() {
self.can_lower_expr_to_const_arg_direct(
body,
DirectConstArgContext::MacrolessMinGenericConstArgs,
)
.is_ok()
} else {
// do not check can_lower_expr_to_const_arg_direct, but rather just
// ExprKind::DirectConstArg, because we don't want e.g.
// `impl<const N: u8> { const C: u8 = N; }` to be a direct-rhs const
matches!(body, Expr { kind: ExprKind::DirectConstArg(_), .. })
}
};
// N.B.: the feature gate for this is generic_const_args, not min_generic_const_args
if self.tcx.features().generic_const_args()
&& let Some(body) = body
&& is_direct(body)
{
hir::ConstItemRhs::Direct(
self.arena.alloc(self.lower_expr_to_const_arg_direct(&body, None)),
)
} else {
hir::ConstItemRhs::Body(self.lower_const_body(span, body.as_deref()))
}
}
(Some(body), ConstItemKind::TypeConst) => hir::ConstItemRhs::Direct(self.arena.alloc(
match self.can_lower_expr_to_const_arg_direct(
&body,
let is_direct = |body| {
if self.tcx.features().macroless_generic_const_args() {
self.can_lower_expr_to_const_arg_direct(
body,
DirectConstArgContext::MacrolessMinGenericConstArgs,
) {
Ok(()) => self.lower_expr_to_const_arg_direct(&body, None),
Err(err) => err.emit(self),
},
)),
(None, ConstItemKind::TypeConst) => {
let const_arg = ConstArg {
hir_id: self.next_id(),
kind: hir::ConstArgKind::Error(
self.dcx().span_delayed_bug(DUMMY_SP, "no block"),
),
span: DUMMY_SP,
};
hir::ConstItemRhs::Direct(self.arena.alloc(const_arg))
)
.is_ok()
} else {
// do not check can_lower_expr_to_const_arg_direct, but rather just
// ExprKind::DirectConstArg, because we don't want e.g.
// `impl<const N: u8> { const C: u8 = N; }` to be a direct-rhs const
matches!(body, Expr { kind: ExprKind::DirectConstArg(_), .. })
}
};
if self.tcx.features().min_generic_const_args()
&& let Some(body) = body
&& is_direct(body)
{
hir::ConstItemRhs::Direct(
self.arena.alloc(self.lower_expr_to_const_arg_direct(&body, None)),
)
} else {
hir::ConstItemRhs::Body(self.lower_const_body(span, body.as_deref()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
// `a::b::Trait(Args)::TraitItem`
Res::Def(DefKind::AssocFn, _)
| Res::Def(DefKind::AssocConst { .. }, _)
| Res::Def(DefKind::AssocConst, _)
| Res::Def(DefKind::AssocTy, _)
if i + 2 == proj_start =>
{
Expand Down
22 changes: 3 additions & 19 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
ast::ItemKind::TyAlias(ast::TyAlias { ty: Some(ty), .. }) => {
self.check_impl_trait(ty, false)
}
ast::ItemKind::Const(ast::ConstItem {
kind: ast::ConstItemKind::TypeConst, ..
}) => {
// Make sure this is only allowed if the feature gate is enabled.
// #![feature(min_generic_const_args)]
gate!(self, min_generic_const_args, i.span, "top-level `type const` are unstable");
}

_ => {}
}
Expand Down Expand Up @@ -350,18 +343,9 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
false
}
ast::AssocItemKind::Const(ast::ConstItem {
body,
kind: ast::ConstItemKind::TypeConst,
..
}) => {
// Make sure this is only allowed if the feature gate is enabled.
// #![feature(min_generic_const_args)]
gate!(self, min_generic_const_args, i.span, "associated `type const` are unstable");
// Make sure associated `type const` defaults in traits are only allowed
// if the feature gate is enabled.
// #![feature(associated_type_defaults)]
if ctxt == AssocCtxt::Trait && body.is_some() {
ast::AssocItemKind::Const(ast::ConstItem { body: Some(_), .. }) => {
if ctxt == AssocCtxt::Trait && attr::contains_name(&i.attrs, sym::rustc_always_gca)
Comment thread
JonathanBrouwer marked this conversation as resolved.
{
gate!(
self,
associated_type_defaults,
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ impl<'a> State<'a> {
generics,
ty,
body,
kind: _,
define_opaque,
}) => {
self.print_item_const(
Expand Down Expand Up @@ -621,7 +620,6 @@ impl<'a> State<'a> {
generics,
ty,
body,
kind: _,
define_opaque,
}) => {
self.print_item_const(
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_attr_ir/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ pub enum AttributeKind {
/// Represents `#[allow_internal_unstable]`.
AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),

/// Represents `#[rustc_always_gca]`
AlwaysGca,

/// Represents `#[automatically_derived]`
AutomaticallyDerived,

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_attr_ir/src/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl AttributeKind {
// tidy-alphabetical-start
AllowInternalUnsafe(..) => Yes,
AllowInternalUnstable(..) => Yes,
AlwaysGca => Yes,
AutomaticallyDerived => Yes,
CfgAttrTrace(..) => Yes,
CfgTrace(..) => Yes,
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ impl NoArgsAttributeParser for ComptimeParser {
const STABILITY: AttributeStability = unstable!(rustc_attrs);
const CREATE: fn(Span) -> AttributeKind = AttributeKind::RustcComptime;
}

pub(crate) struct AlwaysGcaParser;
impl NoArgsAttributeParser for AlwaysGcaParser {
const PATH: &[Symbol] = &[sym::rustc_always_gca];
const ALLOWED_TARGETS: AllowedTargets<'_> =
AllowedTargets::AllowList(&[Allow(Target::AssocConst(AssocCtxt::Trait))]);
const STABILITY: AttributeStability = unstable!(min_generic_const_args);
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::AlwaysGca;
}
1 change: 1 addition & 0 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ attribute_parsers!(
Single<UnrollParser>,
Single<WindowsSubsystemParser>,
Single<WithoutArgs<AllowInternalUnsafeParser>>,
Single<WithoutArgs<AlwaysGcaParser>>,
Single<WithoutArgs<AutomaticallyDerivedParser>>,
Single<WithoutArgs<ColdParser>>,
Single<WithoutArgs<CompilerBuiltinsParser>>,
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,10 +1271,7 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = base.kind else { return };
let (hir::def::Res::Local(_)
| hir::def::Res::Def(
DefKind::Const { .. }
| DefKind::ConstParam
| DefKind::Static { .. }
| DefKind::AssocConst { .. },
DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } | DefKind::AssocConst,
_,
)) = path.res
else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/implied_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub(super) fn mir_borrowck_implied_outlives_bounds<'tcx>(
// - We must compute the normalized signature and then compute implied bounds from that
// in order to connect any unconstrained region vars created during normalization to
// the types of the locals corresponding to the inputs and outputs of the item. #136547
if matches!(tcx.def_kind(body_def_id), DefKind::AssocFn | DefKind::AssocConst { .. }) {
if matches!(tcx.def_kind(body_def_id), DefKind::AssocFn | DefKind::AssocConst) {
for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(body_def_id)) {
let normalized_ty = ocx
.deeply_normalize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
// - We must compute the normalized signature and then compute implied bounds from that
// in order to connect any unconstrained region vars created during normalization to
// the types of the locals corresponding to the inputs and outputs of the item. (#136547)
if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst { .. })
{
if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst) {
for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(defining_ty_def_id)) {
let result: Result<_, ErrorGuaranteed> = self
.infcx
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,8 @@ pub(crate) fn expand(
// Generate anonymous constant serving as container for the allocator methods.
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
let const_body = ecx.expr_block(ecx.block(span, stmts));
let const_item = ecx.item_const(
span,
Ident::new(kw::Underscore, span),
const_ty,
Some(const_body),
ast::ConstItemKind::Body,
);
let const_item =
ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, Some(const_body));
let const_item = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand Down
8 changes: 1 addition & 7 deletions compiler/rustc_builtin_macros/src/eii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,7 @@ fn generate_default_impl(
let anon_mod = |span: Span, stmts: ThinVec<ast::Stmt>| {
let unit = ecx.ty(item_span, ast::TyKind::Tup(ThinVec::new()));
let underscore = Ident::new(kw::Underscore, item_span);
ecx.item_const(
span,
underscore,
unit,
Some(ecx.expr_block(ecx.block(span, stmts))),
ast::ConstItemKind::Body,
)
ecx.item_const(span, underscore, unit, Some(ecx.expr_block(ecx.block(span, stmts))))
};

// const _: () = {
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ pub(crate) fn expand(
// Generate anonymous constant serving as container for the allocator methods.
let const_ty = ecx.ty(ty_span, TyKind::Tup(ThinVec::new()));
let const_body = ecx.expr_block(ecx.block(span, stmts));
let const_item = ecx.item_const(
span,
Ident::new(kw::Underscore, span),
const_ty,
Some(const_body),
ast::ConstItemKind::Body,
);
let const_item =
ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, Some(const_body));
let const_item = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
Ident::new(kw::Underscore, span),
cx.ty(span, ast::TyKind::Tup(ThinVec::new())),
Some(block),
ast::ConstItemKind::Body,
);

// Integrate the new item into existing module structures.
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ pub(crate) fn expand_test_or_bench(
generics: ast::Generics::default(),
ty: cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
define_opaque: None,
kind: ast::ConstItemKind::Body,
// test::TestDescAndFn {
body: Some(
cx.expr_struct(
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ fn setup_for_eval<'tcx>(
cid.promoted.is_some()
|| matches!(
ecx.tcx.def_kind(cid.instance.def_id()),
DefKind::Const { .. }
DefKind::Const
| DefKind::Static { .. }
| DefKind::ConstParam
| DefKind::AnonConst
| DefKind::AssocConst { .. }
| DefKind::AssocConst
),
"Unexpected DefKind: {:?}",
ecx.tcx.def_kind(cid.instance.def_id())
Expand Down Expand Up @@ -441,9 +441,7 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
) -> Result<R, ErrorHandled> {
let def = cid.instance.def.def_id();
// directly represented consts don't have bodies
if cfg!(debug_assertions)
&& matches!(tcx.def_kind(def), DefKind::Const { .. } | DefKind::AssocConst { .. })
{
if cfg!(debug_assertions) && matches!(tcx.def_kind(def), DefKind::Const | DefKind::AssocConst) {
debug_assert!(
tcx.const_of_item(def).is_none(),
"CTFE tried to evaluate directly represented const item: {def:?}"
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,6 @@ impl<'a> ExtCtxt<'a> {
ident: Ident,
ty: Box<ast::Ty>,
body: Option<Box<Expr>>,
kind: ast::ConstItemKind,
) -> Box<ast::Item> {
let defaultness = ast::Defaultness::Implicit;
self.item(
Expand All @@ -734,7 +733,6 @@ impl<'a> ExtCtxt<'a> {
generics: ast::Generics::default(),
ty,
body,
kind,
define_opaque: None,
}
.into(),
Expand Down
Loading
Loading