1
Fork 0

Add ConstArgKind::Path and make ConstArg its own HIR node

This is a very large commit since a lot needs to be changed in order to
make the tests pass. The salient changes are:

- `ConstArgKind` gets a new `Path` variant, and all const params are now
  represented using it. Non-param paths still use `ConstArgKind::Anon`
  to prevent this change from getting too large, but they will soon use
  the `Path` variant too.

- `ConstArg` gets a distinct `hir_id` field and its own variant in
  `hir::Node`. This affected many parts of the compiler that expected
  the parent of an `AnonConst` to be the containing context (e.g., an
  array repeat expression). They have been changed to check the
  "grandparent" where necessary.

- Some `ast::AnonConst`s now have their `DefId`s created in
  rustc_ast_lowering rather than `DefCollector`. This is because in some
  cases they will end up becoming a `ConstArgKind::Path` instead, which
  has no `DefId`. We have to solve this in a hacky way where we guess
  whether the `AnonConst` could end up as a path const since we can't
  know for sure until after name resolution (`N` could refer to a free
  const or a nullary struct). If it has no chance as being a const
  param, then we create a `DefId` in `DefCollector` -- otherwise we
  decide during ast_lowering. This will have to be updated once all path
  consts use `ConstArgKind::Path`.

- We explicitly use `ConstArgHasType` for array lengths, rather than
  implicitly relying on anon const type feeding -- this is due to the
  addition of `ConstArgKind::Path`.

- Some tests have their outputs changed, but the changes are for the
  most part minor (including removing duplicate or almost-duplicate
  errors). One test now ICEs, but it is for an incomplete, unstable
  feature and is now tracked at #127009.
This commit is contained in:
Noah Lev 2024-07-16 19:07:36 -07:00
parent 1c49d406b6
commit 37ed7a4438
61 changed files with 542 additions and 415 deletions

View file

@ -228,34 +228,51 @@ impl<'hir> PathSegment<'hir> {
}
}
/// A constant that enters the type system, e.g. through const generics or even
/// array lengths.
///
/// These are distinct from [`AnonConst`] in part because with the plan for
/// `min_generic_const_args`, arbitrary anonymous constants (like `Foo<{N + 1}>`)
/// will *not* be allowed to use generic parameters. Instead, it will be necessary
/// to add indirection using a free constant that itself has const parameters.
///
/// So, `ConstArg` (specifically, [`ConstArgKind`]) distinguishes between const args
/// that are [just paths](ConstArgKind::Path) (currently just bare const params)
/// versus const args that are literals or have arbitrary computations (e.g., `{ 1 + 3}`).
#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct ConstArg<'hir> {
#[stable_hasher(ignore)]
pub hir_id: HirId,
pub kind: ConstArgKind<'hir>,
/// Indicates whether this comes from a `~const` desugaring.
pub is_desugared_from_effects: bool,
}
impl<'hir> ConstArg<'hir> {
pub fn span(&self) -> Span {
pub fn anon_const_hir_id(&self) -> Option<HirId> {
match self.kind {
ConstArgKind::Anon(anon) => anon.span,
ConstArgKind::Anon(ac) => Some(ac.hir_id),
_ => None,
}
}
// FIXME: convert to field, where ConstArg has its own HirId
pub fn hir_id(&self) -> HirId {
self.anon_const_hir_id()
}
pub fn anon_const_hir_id(&self) -> HirId {
pub fn span(&self) -> Span {
match self.kind {
ConstArgKind::Anon(anon) => anon.hir_id,
ConstArgKind::Path(path) => path.span(),
ConstArgKind::Anon(anon) => anon.span,
}
}
}
/// See [`ConstArg`].
#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub enum ConstArgKind<'hir> {
/// **Note:** Currently this is only used for bare const params
/// (`N` where `fn foo<const N: usize>(...)`),
/// not paths to any const (`N` where `const N: usize = ...`).
///
/// However, in the future, we'll be using it for all of those.
Path(QPath<'hir>),
Anon(&'hir AnonConst),
}
@ -293,7 +310,7 @@ impl GenericArg<'_> {
match self {
GenericArg::Lifetime(l) => l.hir_id,
GenericArg::Type(t) => t.hir_id,
GenericArg::Const(c) => c.hir_id(),
GenericArg::Const(c) => c.hir_id,
GenericArg::Infer(i) => i.hir_id,
}
}
@ -1628,8 +1645,9 @@ pub enum ArrayLen<'hir> {
impl ArrayLen<'_> {
pub fn hir_id(&self) -> HirId {
match self {
ArrayLen::Infer(InferArg { hir_id, .. }) => *hir_id,
ArrayLen::Body(ct) => ct.hir_id(),
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(ConstArg { hir_id, .. }) => {
*hir_id
}
}
}
}
@ -3712,6 +3730,7 @@ pub enum Node<'hir> {
Field(&'hir FieldDef<'hir>),
AnonConst(&'hir AnonConst),
ConstBlock(&'hir ConstBlock),
ConstArg(&'hir ConstArg<'hir>),
Expr(&'hir Expr<'hir>),
ExprField(&'hir ExprField<'hir>),
Stmt(&'hir Stmt<'hir>),
@ -3773,6 +3792,7 @@ impl<'hir> Node<'hir> {
Node::Param(..)
| Node::AnonConst(..)
| Node::ConstBlock(..)
| Node::ConstArg(..)
| Node::Expr(..)
| Node::Stmt(..)
| Node::Block(..)

View file

@ -732,8 +732,10 @@ pub fn walk_const_arg<'v, V: Visitor<'v>>(
visitor: &mut V,
const_arg: &'v ConstArg<'v>,
) -> V::Result {
match const_arg.kind {
ConstArgKind::Anon(anon) => visitor.visit_anon_const(anon),
try_visit!(visitor.visit_id(const_arg.hir_id));
match &const_arg.kind {
ConstArgKind::Path(qpath) => visitor.visit_qpath(qpath, const_arg.hir_id, qpath.span()),
ConstArgKind::Anon(anon) => visitor.visit_anon_const(*anon),
}
}