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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 7 additions & 35 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4010,44 +4010,16 @@ pub struct ConstItem {
pub ident: Ident,
pub generics: Generics,
pub ty: Box<Ty>,
pub rhs_kind: ConstItemRhsKind,
pub body: Option<Box<Expr>>,
#[visitable(ignore)]
pub kind: ConstItemKind,

@khyperia khyperia Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these names are all bikesheddable, fwiw, just kinda kept the names that were there before.

  • body or expr?
  • ConstItemKind::Body or ConstItemKind::Normal? Or Regular or Standard or Plain or...
  • kind or something specific to, like, is_type_const: TypeConstness::{Yes, No}?
  • or just, is_type_const: bool?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like enums instead of bools ✨

pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
pub enum ConstItemRhsKind {
Body { rhs: Option<Box<Expr>> },
TypeConst { rhs: Option<AnonConst> },
}

impl ConstItemRhsKind {
pub fn new_body(rhs: Box<Expr>) -> Self {
Self::Body { rhs: Some(rhs) }
}

pub fn span(&self) -> Option<Span> {
Some(self.expr()?.span)
}

pub fn expr(&self) -> Option<&Expr> {
match self {
Self::Body { rhs: Some(body) } => Some(&body),
Self::TypeConst { rhs: Some(anon) } => Some(&anon.value),
_ => None,
}
}

pub fn has_expr(&self) -> bool {
match self {
Self::Body { rhs: Some(_) } => true,
Self::TypeConst { rhs: Some(_) } => true,
_ => false,
}
}

pub fn is_type_const(&self) -> bool {
matches!(self, &Self::TypeConst { .. })
}
#[derive(Clone, Copy, Encodable, Decodable, Debug, PartialEq, Eq)]
pub enum ConstItemKind {
Body,
TypeConst,
}

#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ macro_rules! common_visitor_and_walkers {
Const,
ConstBlockItem,
ConstItem,
ConstItemRhsKind,
Defaultness,
Delegation,
DelegationMac,
Expand Down
21 changes: 12 additions & 9 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
ident,
generics,
ty,
rhs_kind,
body,
kind,
define_opaque,
}) => {
let ident = self.lower_ident(*ident);
Expand All @@ -282,7 +283,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
let rhs = this.lower_const_item_rhs(rhs_kind, span);
let rhs = this.lower_const_item_rhs(body, *kind, span);
(ty, rhs)
},
);
Expand Down Expand Up @@ -918,7 +919,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
ident,
generics,
ty,
rhs_kind,
body,
kind,
define_opaque,
..
}) => {
Expand All @@ -931,8 +933,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
// Trait associated consts don't need an expression/body.
let rhs = if rhs_kind.has_expr() {
Some(this.lower_const_item_rhs(rhs_kind, i.span))
let rhs = if body.is_some() {
Some(this.lower_const_item_rhs(body, *kind, i.span))
} else {
None
};
Expand All @@ -941,7 +943,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
);

if define_opaque.is_some() {
if rhs_kind.has_expr() {
if body.is_some() {
self.lower_define_opaque(hir_id, &define_opaque);
} else {
self.dcx().span_err(
Expand All @@ -951,7 +953,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

(*ident, generics, kind, rhs_kind.has_expr())
(*ident, generics, kind, body.is_some())
}
AssocItemKind::Fn(Fn { sig, ident, generics, body: None, define_opaque, .. }) => {
// FIXME(contracts): Deny contract here since it won't apply to
Expand Down Expand Up @@ -1180,7 +1182,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
ident,
generics,
ty,
rhs_kind,
body,
kind,
define_opaque,
..
}) => (
Expand All @@ -1194,7 +1197,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
);
this.lower_define_opaque(hir_id, &define_opaque);
let rhs = this.lower_const_item_rhs(rhs_kind, i.span);
let rhs = this.lower_const_item_rhs(body, *kind, i.span);
hir::ImplItemKind::Const(ty, rhs)
},
),
Expand Down
20 changes: 9 additions & 11 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2671,28 +2671,26 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_const_item_rhs(
&mut self,
rhs_kind: &ConstItemRhsKind,
body: &Option<Box<Expr>>,
kind: ConstItemKind,
span: Span,
) -> hir::ConstItemRhs<'hir> {
match rhs_kind {
ConstItemRhsKind::Body { rhs: Some(body) } => {
hir::ConstItemRhs::Body(self.lower_const_body(span, Some(body)))
match (body, kind) {
(body, ConstItemKind::Body) => {
hir::ConstItemRhs::Body(self.lower_const_body(span, body.as_deref()))
}
ConstItemRhsKind::Body { rhs: None } => {
hir::ConstItemRhs::Body(self.lower_const_body(span, None))
}
ConstItemRhsKind::TypeConst { rhs: Some(anon) } => {
(Some(body), ConstItemKind::TypeConst) => {
hir::ConstItemRhs::TypeConst(self.arena.alloc(
match self.can_lower_expr_to_const_arg_direct(
&anon.value,
&body,
DirectConstArgContext::MacrolessMinGenericConstArgs,
) {
Ok(()) => self.lower_expr_to_const_arg_direct(&anon.value, Some(anon.id)),
Ok(()) => self.lower_expr_to_const_arg_direct(&body, None),
Err(err) => err.emit(self),
},
))
}
ConstItemRhsKind::TypeConst { rhs: None } => {
(None, ConstItemKind::TypeConst) => {
let const_arg = ConstArg {
hir_id: self.next_id(),
kind: hir::ConstArgKind::Error(
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,9 @@ impl Visitor<'_> for AstValidator<'_> {
visit::walk_item(this, item)
});
}
ItemKind::Const(ConstItem { defaultness, ident, rhs_kind, .. }) => {
ItemKind::Const(ConstItem { defaultness, ident, body, .. }) => {
self.check_defaultness(item.span, *defaultness, AllowDefault::No, AllowFinal::No);
if !rhs_kind.has_expr() {
if body.is_none() {
self.dcx().emit_err(diagnostics::ConstWithoutBody {
span: item.span,
replace_span: self.ending_semi_or_hi(item.span),
Expand Down Expand Up @@ -1941,8 +1941,8 @@ impl Visitor<'_> for AstValidator<'_> {

if let AssocCtxt::Impl { .. } = ctxt {
match &item.kind {
AssocItemKind::Const(ConstItem { rhs_kind, .. }) => {
if !rhs_kind.has_expr() {
AssocItemKind::Const(ConstItem { body, .. }) => {
if body.is_none() {
self.dcx().emit_err(diagnostics::AssocConstWithoutBody {
span: item.span,
replace_span: self.ending_semi_or_hi(item.span),
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
self.check_impl_trait(ty, false)
}
ast::ItemKind::Const(ast::ConstItem {
rhs_kind: ast::ConstItemRhsKind::TypeConst { .. },
..
kind: ast::ConstItemKind::TypeConst, ..
}) => {
// Make sure this is only allowed if the feature gate is enabled.
// #![feature(min_generic_const_args)]
Expand Down Expand Up @@ -400,7 +399,8 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
false
}
ast::AssocItemKind::Const(ast::ConstItem {
rhs_kind: ast::ConstItemRhsKind::TypeConst { rhs },
body,
kind: ast::ConstItemKind::TypeConst,
..
}) => {
// Make sure this is only allowed if the feature gate is enabled.
Expand All @@ -409,7 +409,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
// 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 && rhs.is_some() {
if ctxt == AssocCtxt::Trait && body.is_some() {
gate!(
self,
associated_type_defaults,
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,16 @@ impl<'a> State<'a> {
ident,
generics,
ty,
rhs_kind,
body,
kind: _,
define_opaque,
}) => {
self.print_item_const(
*ident,
None,
generics,
ty,
rhs_kind.expr(),
body.as_deref(),
&item.vis,
ast::Safety::Default,
*defaultness,
Expand Down Expand Up @@ -617,15 +618,16 @@ impl<'a> State<'a> {
ident,
generics,
ty,
rhs_kind,
body,
kind: _,
define_opaque,
}) => {
self.print_item_const(
*ident,
None,
generics,
ty,
rhs_kind.expr(),
body.as_deref(),
vis,
ast::Safety::Default,
*defaultness,
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ 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 = ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts)));
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
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 = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/eii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ fn generate_default_impl(
span,
underscore,
unit,
ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts))),
Some(ecx.expr_block(ecx.block(span, stmts))),
ast::ConstItemKind::Body,
)
};

Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ 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 = ast::ConstItemRhsKind::new_body(ecx.expr_block(ecx.block(span, stmts)));
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
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 = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_builtin_macros/src/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,15 +361,16 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
cx.attr_nested_word(sym::allow, sym::deprecated, span),
]);

let block = ast::ConstItemRhsKind::new_body(cx.expr_block(
let block = cx.expr_block(
cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
));
);

let anon_constant = cx.item_const(
span,
Ident::new(kw::Underscore, span),
cx.ty(span, ast::TyKind::Tup(ThinVec::new())),
block,
Some(block),
ast::ConstItemKind::Body,
);

// Integrate the new item into existing module structures.
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,9 @@ 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 {
rhs_kind: ast::ConstItemRhsKind::new_body(
body: Some(
cx.expr_struct(
sp,
test_path("TestDescAndFn"),
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ impl<'a> ExtCtxt<'a> {
span: Span,
ident: Ident,
ty: Box<ast::Ty>,
rhs_kind: ast::ConstItemRhsKind,
body: Option<Box<Expr>>,
kind: ast::ConstItemKind,
) -> Box<ast::Item> {
let defaultness = ast::Defaultness::Implicit;
self.item(
Expand All @@ -732,7 +733,8 @@ impl<'a> ExtCtxt<'a> {
// FIXME(generic_const_items): Pass the generics as a parameter.
generics: ast::Generics::default(),
ty,
rhs_kind,
body,
kind,
define_opaque: None,
}
.into(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ trait UnusedDelimLint {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
use ast::ItemKind::*;

let expr = if let Const(ast::ConstItem { rhs_kind, .. }) = &item.kind {
if let Some(e) = rhs_kind.expr() { e } else { return }
let expr = if let Const(ast::ConstItem { body: Some(expr), .. }) = &item.kind {
expr
} else if let Static(ast::StaticItem { expr: Some(expr), .. }) = &item.kind {
expr
} else {
Expand Down
Loading
Loading