Skip to content

Some hir cleanups #124401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 4, 2024
Merged
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
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(super) fn index_hir<'hir>(
if let Node::Err(span) = node.node {
let hir_id = HirId { owner: item.def_id(), local_id };
let msg = format!("ID {hir_id} not encountered when visiting item HIR");
tcx.dcx().span_delayed_bug(*span, msg);
tcx.dcx().span_delayed_bug(span, msg);
}
}

Expand Down Expand Up @@ -375,7 +375,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}
}

fn visit_array_length(&mut self, len: &'hir ArrayLen) {
fn visit_array_length(&mut self, len: &'hir ArrayLen<'hir>) {
match len {
ArrayLen::Infer(inf) => self.insert(inf.span, inf.hir_id, Node::ArrayLenInfer(inf)),
ArrayLen::Body(..) => intravisit::walk_array_len(self, len),
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,11 +1588,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
}),
)),
)),
default: Some(hir::AnonConst {
default: Some(self.arena.alloc(hir::AnonConst {
def_id: anon_const,
hir_id: const_id,
body: const_body,
}),
span,
})),
is_host_effect: true,
},
colon_span: None,
Expand Down
26 changes: 14 additions & 12 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,14 +1181,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
tokens: None,
};

let ct = self.with_new_scopes(span, |this| hir::AnonConst {
def_id,
hir_id: this.lower_node_id(node_id),
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
let ct = self.with_new_scopes(span, |this| {
self.arena.alloc(hir::AnonConst {
def_id,
hir_id: this.lower_node_id(node_id),
body: this
.lower_const_body(path_expr.span, Some(&path_expr)),
span,
})
});
return GenericArg::Const(ConstArg {
value: ct,
span,
is_desugared_from_effects: false,
});
}
Expand All @@ -1200,7 +1203,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
value: self.lower_anon_const(ct),
span: self.lower_span(ct.value.span),
is_desugared_from_effects: false,
}),
}
Expand Down Expand Up @@ -2318,7 +2320,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen<'hir> {
match c.value.kind {
ExprKind::Underscore => {
if self.tcx.features().generic_arg_infer {
Expand All @@ -2341,12 +2343,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}

fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
self.with_new_scopes(c.value.span, |this| hir::AnonConst {
fn lower_anon_const(&mut self, c: &AnonConst) -> &'hir hir::AnonConst {
self.arena.alloc(self.with_new_scopes(c.value.span, |this| hir::AnonConst {
def_id: this.local_def_id(c.id),
hir_id: this.lower_node_id(c.id),
body: this.lower_const_body(c.value.span, Some(&c.value)),
})
span: this.lower_span(c.value.span),
}))
}

fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
Expand Down Expand Up @@ -2653,8 +2656,7 @@ impl<'hir> GenericArgsCtor<'hir> {

lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
self.args.push(hir::GenericArg::Const(hir::ConstArg {
value: hir::AnonConst { def_id, hir_id, body },
span,
value: lcx.arena.alloc(hir::AnonConst { def_id, hir_id, body, span }),
is_desugared_from_effects: true,
}))
}
Expand Down
50 changes: 23 additions & 27 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ impl<'hir> PathSegment<'hir> {
}

#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct ConstArg {
pub value: AnonConst,
pub span: Span,
pub struct ConstArg<'hir> {
pub value: &'hir AnonConst,
/// Indicates whether this comes from a `~const` desugaring.
pub is_desugared_from_effects: bool,
}
Expand All @@ -253,7 +252,7 @@ impl InferArg {
pub enum GenericArg<'hir> {
Lifetime(&'hir Lifetime),
Type(&'hir Ty<'hir>),
Const(ConstArg),
Const(ConstArg<'hir>),
Infer(InferArg),
}

Expand All @@ -262,7 +261,7 @@ impl GenericArg<'_> {
match self {
GenericArg::Lifetime(l) => l.ident.span,
GenericArg::Type(t) => t.span,
GenericArg::Const(c) => c.span,
GenericArg::Const(c) => c.value.span,
GenericArg::Infer(i) => i.span,
}
}
Expand Down Expand Up @@ -491,7 +490,7 @@ pub enum GenericParamKind<'hir> {
Const {
ty: &'hir Ty<'hir>,
/// Optional default value for the const generic param
default: Option<AnonConst>,
default: Option<&'hir AnonConst>,
is_host_effect: bool,
},
}
Expand Down Expand Up @@ -1563,12 +1562,12 @@ impl fmt::Display for ConstContext {
pub type Lit = Spanned<LitKind>;

#[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum ArrayLen {
pub enum ArrayLen<'hir> {
Infer(InferArg),
Body(AnonConst),
Body(&'hir AnonConst),
}

impl ArrayLen {
impl ArrayLen<'_> {
pub fn hir_id(&self) -> HirId {
match self {
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
Expand All @@ -1591,6 +1590,7 @@ pub struct AnonConst {
pub hir_id: HirId,
pub def_id: LocalDefId,
pub body: BodyId,
pub span: Span,
}

/// An inline constant expression `const { something }`.
Expand Down Expand Up @@ -1965,7 +1965,7 @@ pub enum ExprKind<'hir> {
///
/// E.g., `[1; 5]`. The first expression is the element
/// to be repeated; the second is the number of times to repeat it.
Repeat(&'hir Expr<'hir>, ArrayLen),
Repeat(&'hir Expr<'hir>, ArrayLen<'hir>),

/// A suspension point for coroutines (i.e., `yield <expr>`).
Yield(&'hir Expr<'hir>, YieldSource),
Expand Down Expand Up @@ -2345,7 +2345,7 @@ pub struct TypeBinding<'hir> {
#[derive(Debug, Clone, Copy, HashStable_Generic)]
pub enum Term<'hir> {
Ty(&'hir Ty<'hir>),
Const(AnonConst),
Const(&'hir AnonConst),
}

impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
Expand All @@ -2354,8 +2354,8 @@ impl<'hir> From<&'hir Ty<'hir>> for Term<'hir> {
}
}

impl<'hir> From<AnonConst> for Term<'hir> {
fn from(c: AnonConst) -> Self {
impl<'hir> From<&'hir AnonConst> for Term<'hir> {
fn from(c: &'hir AnonConst) -> Self {
Term::Const(c)
}
}
Expand Down Expand Up @@ -2646,7 +2646,7 @@ pub enum TyKind<'hir> {
/// A variable length slice (i.e., `[T]`).
Slice(&'hir Ty<'hir>),
/// A fixed length array (i.e., `[T; n]`).
Array(&'hir Ty<'hir>, ArrayLen),
Array(&'hir Ty<'hir>, ArrayLen<'hir>),
/// A raw pointer (i.e., `*const T` or `*mut T`).
Ptr(MutTy<'hir>),
/// A reference (i.e., `&'a T` or `&'a mut T`).
Expand Down Expand Up @@ -2675,7 +2675,7 @@ pub enum TyKind<'hir> {
/// where `Bound` is a trait or a lifetime.
TraitObject(&'hir [PolyTraitRef<'hir>], &'hir Lifetime, TraitObjectSyntax),
/// Unused for now.
Typeof(AnonConst),
Typeof(&'hir AnonConst),
/// `TyKind::Infer` means the type should be inferred instead of it having been
/// specified. This can appear anywhere in a type.
Infer,
Expand Down Expand Up @@ -2708,10 +2708,10 @@ pub enum InlineAsmOperand<'hir> {
out_expr: Option<&'hir Expr<'hir>>,
},
Const {
anon_const: AnonConst,
anon_const: &'hir AnonConst,
},
SymFn {
anon_const: AnonConst,
anon_const: &'hir AnonConst,
},
SymStatic {
path: QPath<'hir>,
Expand Down Expand Up @@ -2913,7 +2913,7 @@ pub struct Variant<'hir> {
/// Fields and constructor id of the variant.
pub data: VariantData<'hir>,
/// Explicit discriminant (e.g., `Foo = 1`).
pub disr_expr: Option<AnonConst>,
pub disr_expr: Option<&'hir AnonConst>,
/// Span
pub span: Span,
}
Expand Down Expand Up @@ -3442,15 +3442,13 @@ impl<'hir> OwnerNode<'hir> {
}
}

// Span by reference to pass to `Node::Err`.
#[allow(rustc::pass_by_value)]
pub fn span(&self) -> &'hir Span {
pub fn span(&self) -> Span {
match self {
OwnerNode::Item(Item { span, .. })
| OwnerNode::ForeignItem(ForeignItem { span, .. })
| OwnerNode::ImplItem(ImplItem { span, .. })
| OwnerNode::TraitItem(TraitItem { span, .. }) => span,
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => inner_span,
| OwnerNode::TraitItem(TraitItem { span, .. }) => *span,
OwnerNode::Crate(Mod { spans: ModSpans { inner_span, .. }, .. }) => *inner_span,
OwnerNode::Synthetic => unreachable!(),
}
}
Expand Down Expand Up @@ -3595,9 +3593,7 @@ pub enum Node<'hir> {
PreciseCapturingNonLifetimeArg(&'hir PreciseCapturingNonLifetimeArg),
// Created by query feeding
Synthetic,
// Span by reference to minimize `Node`'s size
#[allow(rustc::pass_by_value)]
Err(&'hir Span),
Err(Span),
}

impl<'hir> Node<'hir> {
Expand Down Expand Up @@ -3833,7 +3829,7 @@ mod size_asserts {
static_assert_size!(FnDecl<'_>, 40);
static_assert_size!(ForeignItem<'_>, 72);
static_assert_size!(ForeignItemKind<'_>, 40);
static_assert_size!(GenericArg<'_>, 32);
static_assert_size!(GenericArg<'_>, 24);
static_assert_size!(GenericBound<'_>, 48);
static_assert_size!(Generics<'_>, 56);
static_assert_size!(Impl<'_>, 80);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub trait Visitor<'v>: Sized {
fn visit_pat_field(&mut self, f: &'v PatField<'v>) -> Self::Result {
walk_pat_field(self, f)
}
fn visit_array_length(&mut self, len: &'v ArrayLen) -> Self::Result {
fn visit_array_length(&mut self, len: &'v ArrayLen<'v>) -> Self::Result {
walk_array_len(self, len)
}
fn visit_anon_const(&mut self, c: &'v AnonConst) -> Self::Result {
Expand Down Expand Up @@ -703,7 +703,7 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
visitor.visit_pat(field.pat)
}

pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) -> V::Result {
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen<'v>) -> V::Result {
match len {
// FIXME: Use `visit_infer` here.
ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
_ => {}
}
}
fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
fn visit_array_length(&mut self, length: &'v hir::ArrayLen<'v>) {
if let hir::ArrayLen::Infer(inf) = length {
self.0.push(inf.span);
}
Expand Down
22 changes: 9 additions & 13 deletions compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
let hir_id = tcx.local_def_id_to_hir_id(def_id);

let node = tcx.hir_node(hir_id);
let Node::AnonConst(_) = node else {
let Node::AnonConst(&AnonConst { span, .. }) = node else {
span_bug!(
tcx.def_span(def_id),
"expected anon const in `anon_const_type_of`, got {node:?}"
Expand Down Expand Up @@ -134,7 +134,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
// I dont think it's possible to reach this but I'm not 100% sure - BoxyUwU
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
"unexpected non-GAT usage of an anon const",
);
}
Expand All @@ -152,7 +152,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
let Some(type_dependent_def) = tables.type_dependent_def_id(parent_node_id) else {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
format!("unable to find type-dependent def for {parent_node_id:?}"),
);
};
Expand Down Expand Up @@ -194,15 +194,15 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
} else {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
format!("unable to find const parent for {hir_id} in pat {pat:?}"),
);
}
}
_ => {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
format!("unexpected const parent path {parent_node:?}"),
);
}
Expand All @@ -226,19 +226,15 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
.map(|idx| (idx, seg))
})
}) else {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
"no arg matching AnonConst in path",
);
return Ty::new_error_with_message(tcx, span, "no arg matching AnonConst in path");
};

let generics = match tcx.res_generics_def_id(segment.res) {
Some(def_id) => tcx.generics_of(def_id),
None => {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
format!("unexpected anon const res {:?} in path: {:?}", segment.res, path),
);
}
Expand All @@ -250,7 +246,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
_ => {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
format!("unexpected const parent in type_of(): {parent_node:?}"),
);
}
Expand Down Expand Up @@ -278,7 +274,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
} else {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
span,
format!("const generic parameter not found in {generics:?} at position {arg_idx:?}"),
);
}
Expand Down
Loading
Loading