Skip to content

Commit 5bc58c7

Browse files
committed
Auto merge of #139558 - camelid:mgca-const-items, r=oli-obk,BoxyUwU
mgca: Add ConstArg representation for const items tracking issue: rust-lang/rust#132980 fixes rust-lang/rust#131046 fixes rust-lang/rust#134641 As part of implementing `min_generic_const_args`, we need to distinguish const items that can be used in the type system, such as in associated const equality projections, from const items containing arbitrary const code, which must be kept out of the type system. Specifically, all "type consts" must be either concrete (no generics) or generic with a trivial expression like `N` or a path to another type const item. To syntactically distinguish these cases, we require, for now at least, that users annotate all type consts with the `#[type_const]` attribute. Then, we validate that the const's right-hand side is indeed eligible to be a type const and represent it differently in the HIR. We accomplish this representation using a new `ConstItemRhs` enum in the HIR, and a similar but simpler enum in the AST. When `#[type_const]` is **not** applied to a const (e.g. on stable), we represent const item right-hand sides (rhs's) as HIR bodies, like before. However, when the attribute is applied, we instead lower to a `hir::ConstArg`. This syntactically distinguishes between trivial const args (paths) and arbitrary expressions, which are represented using `AnonConst`s. Then in `generics_of`, we can take advantage of the existing machinery to bar the `AnonConst` rhs's from using parent generics.
2 parents eaaa7da + 70f6164 commit 5bc58c7

1 file changed

Lines changed: 42 additions & 32 deletions

File tree

src/items.rs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,37 +2004,37 @@ pub(crate) struct StaticParts<'a> {
20042004
generics: Option<&'a ast::Generics>,
20052005
ty: &'a ast::Ty,
20062006
mutability: ast::Mutability,
2007-
expr_opt: Option<&'a Box<ast::Expr>>,
2007+
expr_opt: Option<&'a ast::Expr>,
20082008
defaultness: Option<ast::Defaultness>,
20092009
span: Span,
20102010
}
20112011

20122012
impl<'a> StaticParts<'a> {
20132013
pub(crate) fn from_item(item: &'a ast::Item) -> Self {
2014-
let (defaultness, prefix, safety, ident, ty, mutability, expr, generics) = match &item.kind
2015-
{
2016-
ast::ItemKind::Static(s) => (
2017-
None,
2018-
"static",
2019-
s.safety,
2020-
s.ident,
2021-
&s.ty,
2022-
s.mutability,
2023-
&s.expr,
2024-
None,
2025-
),
2026-
ast::ItemKind::Const(c) => (
2027-
Some(c.defaultness),
2028-
"const",
2029-
ast::Safety::Default,
2030-
c.ident,
2031-
&c.ty,
2032-
ast::Mutability::Not,
2033-
&c.expr,
2034-
Some(&c.generics),
2035-
),
2036-
_ => unreachable!(),
2037-
};
2014+
let (defaultness, prefix, safety, ident, ty, mutability, expr_opt, generics) =
2015+
match &item.kind {
2016+
ast::ItemKind::Static(s) => (
2017+
None,
2018+
"static",
2019+
s.safety,
2020+
s.ident,
2021+
&s.ty,
2022+
s.mutability,
2023+
s.expr.as_deref(),
2024+
None,
2025+
),
2026+
ast::ItemKind::Const(c) => (
2027+
Some(c.defaultness),
2028+
"const",
2029+
ast::Safety::Default,
2030+
c.ident,
2031+
&c.ty,
2032+
ast::Mutability::Not,
2033+
c.rhs.as_ref().map(|rhs| rhs.expr()),
2034+
Some(&c.generics),
2035+
),
2036+
_ => unreachable!(),
2037+
};
20382038
StaticParts {
20392039
prefix,
20402040
safety,
@@ -2043,15 +2043,20 @@ impl<'a> StaticParts<'a> {
20432043
generics,
20442044
ty,
20452045
mutability,
2046-
expr_opt: expr.as_ref(),
2046+
expr_opt,
20472047
defaultness,
20482048
span: item.span,
20492049
}
20502050
}
20512051

20522052
pub(crate) fn from_trait_item(ti: &'a ast::AssocItem, ident: Ident) -> Self {
20532053
let (defaultness, ty, expr_opt, generics) = match &ti.kind {
2054-
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
2054+
ast::AssocItemKind::Const(c) => (
2055+
c.defaultness,
2056+
&c.ty,
2057+
c.rhs.as_ref().map(|rhs| rhs.expr()),
2058+
Some(&c.generics),
2059+
),
20552060
_ => unreachable!(),
20562061
};
20572062
StaticParts {
@@ -2062,15 +2067,20 @@ impl<'a> StaticParts<'a> {
20622067
generics,
20632068
ty,
20642069
mutability: ast::Mutability::Not,
2065-
expr_opt: expr_opt.as_ref(),
2070+
expr_opt,
20662071
defaultness: Some(defaultness),
20672072
span: ti.span,
20682073
}
20692074
}
20702075

20712076
pub(crate) fn from_impl_item(ii: &'a ast::AssocItem, ident: Ident) -> Self {
2072-
let (defaultness, ty, expr, generics) = match &ii.kind {
2073-
ast::AssocItemKind::Const(c) => (c.defaultness, &c.ty, &c.expr, Some(&c.generics)),
2077+
let (defaultness, ty, expr_opt, generics) = match &ii.kind {
2078+
ast::AssocItemKind::Const(c) => (
2079+
c.defaultness,
2080+
&c.ty,
2081+
c.rhs.as_ref().map(|rhs| rhs.expr()),
2082+
Some(&c.generics),
2083+
),
20742084
_ => unreachable!(),
20752085
};
20762086
StaticParts {
@@ -2081,7 +2091,7 @@ impl<'a> StaticParts<'a> {
20812091
generics,
20822092
ty,
20832093
mutability: ast::Mutability::Not,
2084-
expr_opt: expr.as_ref(),
2094+
expr_opt,
20852095
defaultness: Some(defaultness),
20862096
span: ii.span,
20872097
}
@@ -2144,7 +2154,7 @@ fn rewrite_static(
21442154
rewrite_assign_rhs_with_comments(
21452155
context,
21462156
&lhs,
2147-
&**expr,
2157+
expr,
21482158
Shape::legacy(remaining_width, offset.block_only()),
21492159
&RhsAssignKind::Expr(&expr.kind, expr.span),
21502160
RhsTactics::Default,

0 commit comments

Comments
 (0)