hir: Create hir::ConstArgKind enum

This will allow lowering const params to a dedicated enum variant, rather
than to an `AnonConst` that is later examined during `ty` lowering.
This commit is contained in:
Noah Lev 2024-06-02 23:23:28 -07:00
parent 71f8aed510
commit 11b144aa98
13 changed files with 102 additions and 40 deletions

View File

@ -53,7 +53,7 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{self as hir};
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir::{
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
};
@ -1203,7 +1203,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
})
});
return GenericArg::Const(ConstArg {
value: ct,
kind: ConstArgKind::Anon(ct),
is_desugared_from_effects: false,
});
}
@ -1214,7 +1214,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
GenericArg::Type(self.lower_ty(ty, itctx))
}
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
value: self.lower_anon_const(ct),
kind: ConstArgKind::Anon(self.lower_anon_const(ct)),
is_desugared_from_effects: false,
}),
}

View File

@ -230,11 +230,30 @@ impl<'hir> PathSegment<'hir> {
#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct ConstArg<'hir> {
pub value: &'hir AnonConst,
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 {
match self.kind {
ConstArgKind::Anon(anon) => anon.span,
}
}
pub fn hir_id(&self) -> HirId {
match self.kind {
ConstArgKind::Anon(anon) => anon.hir_id,
}
}
}
#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub enum ConstArgKind<'hir> {
Anon(&'hir AnonConst),
}
#[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct InferArg {
pub hir_id: HirId,
@ -260,7 +279,7 @@ impl GenericArg<'_> {
match self {
GenericArg::Lifetime(l) => l.ident.span,
GenericArg::Type(t) => t.span,
GenericArg::Const(c) => c.value.span,
GenericArg::Const(c) => c.span(),
GenericArg::Infer(i) => i.span,
}
}
@ -269,7 +288,7 @@ impl GenericArg<'_> {
match self {
GenericArg::Lifetime(l) => l.hir_id,
GenericArg::Type(t) => t.hir_id,
GenericArg::Const(c) => c.value.hir_id,
GenericArg::Const(c) => c.hir_id(),
GenericArg::Infer(i) => i.hir_id,
}
}

View File

@ -347,6 +347,9 @@ pub trait Visitor<'v>: Sized {
fn visit_inline_const(&mut self, c: &'v ConstBlock) -> Self::Result {
walk_inline_const(self, c)
}
fn visit_const_arg(&mut self, c: &'v ConstArg<'v>) -> Self::Result {
walk_const_arg(self, c)
}
fn visit_expr(&mut self, ex: &'v Expr<'v>) -> Self::Result {
walk_expr(self, ex)
}
@ -725,6 +728,15 @@ pub fn walk_inline_const<'v, V: Visitor<'v>>(
visitor.visit_nested_body(constant.body)
}
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),
}
}
pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>) -> V::Result {
try_visit!(visitor.visit_id(expression.hir_id));
match expression.kind {
@ -1216,7 +1228,7 @@ pub fn walk_generic_arg<'v, V: Visitor<'v>>(
match generic_arg {
GenericArg::Lifetime(lt) => visitor.visit_lifetime(lt),
GenericArg::Type(ty) => visitor.visit_ty(ty),
GenericArg::Const(ct) => visitor.visit_anon_const(&ct.value),
GenericArg::Const(ct) => visitor.visit_const_arg(ct),
GenericArg::Infer(inf) => visitor.visit_infer(inf),
}
}

View File

@ -1594,7 +1594,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
i += 1;
}
GenericArg::Const(ct) => {
self.visit_anon_const(&ct.value);
self.visit_const_arg(ct);
i += 1;
}
GenericArg::Infer(inf) => {

View File

@ -8,10 +8,10 @@ use rustc_ast::ast::ParamKindOrd;
use rustc_errors::{
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, MultiSpan,
};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::GenericArg;
use rustc_hir::{self as hir, ConstArgKind};
use rustc_middle::ty::{
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty,
};
@ -113,7 +113,8 @@ fn generic_arg_mismatch_err(
}
}
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
let body = tcx.hir().body(cnst.value.body);
let ConstArgKind::Anon(anon) = cnst.kind;
let body = tcx.hir().body(anon.body);
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
{
if let Res::Def(DefKind::Fn { .. }, id) = path.res {

View File

@ -470,11 +470,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
(&GenericParamDefKind::Type { has_default, .. }, GenericArg::Infer(inf)) => {
handle_ty_args(has_default, &inf.to_ty())
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
let did = ct.value.def_id;
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
ty::Const::from_anon_const(tcx, did).into()
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => match &ct.kind {
hir::ConstArgKind::Anon(anon) => {
let did = anon.def_id;
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
ty::Const::from_anon_const(tcx, did).into()
}
},
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
self.lowerer.ct_infer(Some(param), inf.span).into()
}

View File

@ -10,7 +10,7 @@ use rustc_ast::util::parser::{self, AssocOp, Fixity};
use rustc_ast_pretty::pp::Breaks::{Consistent, Inconsistent};
use rustc_ast_pretty::pp::{self, Breaks};
use rustc_ast_pretty::pprust::{Comments, PrintState};
use rustc_hir as hir;
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir::{
BindingMode, ByRef, GenericArg, GenericBound, GenericParam, GenericParamKind, HirId,
LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term, TraitBoundModifier,
@ -991,6 +991,12 @@ impl<'a> State<'a> {
self.ann.nested(self, Nested::Body(constant.body))
}
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
match &const_arg.kind {
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
}
}
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
self.popen();
self.commasep_exprs(Inconsistent, args);
@ -1679,7 +1685,7 @@ impl<'a> State<'a> {
GenericArg::Lifetime(lt) if !elide_lifetimes => s.print_lifetime(lt),
GenericArg::Lifetime(_) => {}
GenericArg::Type(ty) => s.print_type(ty),
GenericArg::Const(ct) => s.print_anon_const(&ct.value),
GenericArg::Const(ct) => s.print_const_arg(ct),
GenericArg::Infer(_inf) => s.word("_"),
}
});

View File

@ -5,10 +5,10 @@ use crate::rvalue_scopes;
use crate::{BreakableCtxt, Diverges, Expectation, FnCtxt, LoweredTy};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem;
use rustc_hir::{self as hir, ConstArg, ConstArgKind};
use rustc_hir::{ExprKind, GenericArg, HirId, Node, QPath};
use rustc_hir_analysis::hir_ty_lowering::errors::GenericsArgsErrExtend;
use rustc_hir_analysis::hir_ty_lowering::generics::{
@ -466,16 +466,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
pub fn lower_const_arg(&self, hir_ct: &hir::AnonConst, param_def_id: DefId) -> ty::Const<'tcx> {
let did = hir_ct.def_id;
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
let ct = ty::Const::from_anon_const(self.tcx, did);
self.register_wf_obligation(
ct.into(),
self.tcx.hir().span(hir_ct.hir_id),
ObligationCauseCode::WellFormed(None),
);
ct
pub fn lower_const_arg(
&self,
const_arg: &ConstArg<'tcx>,
param_def_id: DefId,
) -> ty::Const<'tcx> {
match &const_arg.kind {
ConstArgKind::Anon(anon) => {
let did = anon.def_id;
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
let ct = ty::Const::from_anon_const(self.tcx, did);
self.register_wf_obligation(
ct.into(),
self.tcx.hir().span(anon.hir_id),
ObligationCauseCode::WellFormed(None),
);
ct
}
}
}
// If the type given by the user has free regions, save it for later, since
@ -1298,7 +1306,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.fcx.lower_ty(ty).raw.into()
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
self.fcx.lower_const_arg(&ct.value, param.def_id).into()
self.fcx.lower_const_arg(ct, param.def_id).into()
}
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
self.fcx.ty_infer(Some(param), inf.span).into()

View File

@ -400,7 +400,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
self.cfcx.lower_ty(ty).raw.into()
}
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
self.cfcx.lower_const_arg(&ct.value, param.def_id).into()
self.cfcx.lower_const_arg(ct, param.def_id).into()
}
(GenericParamDefKind::Type { .. }, GenericArg::Infer(inf)) => {
self.cfcx.ty_infer(Some(param), inf.span).into()

View File

@ -78,7 +78,7 @@ fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
.tcx
.sess
.source_map()
.span_to_snippet(c.value.span)
.span_to_snippet(c.span())
.unwrap_or_else(|_| "_".into()),
GenericArg::Infer(_) => String::from("_"),
})

View File

@ -452,7 +452,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
match ga {
hir::GenericArg::Lifetime(lt) => self.visit_lifetime(lt),
hir::GenericArg::Type(ty) => self.visit_ty(ty),
hir::GenericArg::Const(ct) => self.visit_anon_const(&ct.value),
hir::GenericArg::Const(ct) => self.visit_const_arg(ct),
hir::GenericArg::Infer(inf) => self.visit_infer(inf),
}
}

View File

@ -36,10 +36,10 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_attr as attr;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
use rustc_errors::{codes::*, struct_span_code_err, FatalError};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::PredicateOrigin;
use rustc_hir::{self as hir, ConstArgKind};
use rustc_hir_analysis::lower_ty;
use rustc_middle::metadata::Reexport;
use rustc_middle::middle::resolve_bound_vars as rbv;
@ -285,10 +285,12 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
}
pub(crate) fn clean_const<'tcx>(
constant: &hir::ConstArg<'_>,
constant: &hir::ConstArg<'tcx>,
_cx: &mut DocContext<'tcx>,
) -> Constant {
Constant { kind: ConstantKind::Anonymous { body: constant.value.body } }
match &constant.kind {
ConstArgKind::Anon(anon) => Constant { kind: ConstantKind::Anonymous { body: anon.body } },
}
}
pub(crate) fn clean_middle_const<'tcx>(

View File

@ -7,9 +7,9 @@ use rustc_data_structures::fx::FxHasher;
use rustc_hir::def::Res;
use rustc_hir::MatchSource::TryDesugar;
use rustc_hir::{
ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy,
GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime, LifetimeName, Pat, PatField,
PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind,
ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr,
ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime,
LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind,
};
use rustc_lexer::{tokenize, TokenKind};
use rustc_lint::LateContext;
@ -411,7 +411,7 @@ impl HirEqInterExpr<'_, '_, '_> {
fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
match (left, right) {
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
(GenericArg::Const(l), GenericArg::Const(r)) => self.eq_const_arg(l, r),
(GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
(GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
(GenericArg::Infer(l_inf), GenericArg::Infer(r_inf)) => self.eq_ty(&l_inf.to_ty(), &r_inf.to_ty()),
@ -419,6 +419,12 @@ impl HirEqInterExpr<'_, '_, '_> {
}
}
fn eq_const_arg(&mut self, left: &ConstArg<'_>, right: &ConstArg<'_>) -> bool {
match (&left.kind, &right.kind) {
(ConstArgKind::Anon(l_an), ConstArgKind::Anon(r_an)) => self.eq_body(l_an.body, r_an.body),
}
}
fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
left.res == right.res
}
@ -1134,12 +1140,18 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
self.maybe_typeck_results = old_maybe_typeck_results;
}
fn hash_const_arg(&mut self, const_arg: &ConstArg<'_>) {
match &const_arg.kind {
ConstArgKind::Anon(anon) => self.hash_body(anon.body),
}
}
fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
for arg in arg_list {
match *arg {
GenericArg::Lifetime(l) => self.hash_lifetime(l),
GenericArg::Type(ty) => self.hash_ty(ty),
GenericArg::Const(ref ca) => self.hash_body(ca.value.body),
GenericArg::Const(ref ca) => self.hash_const_arg(ca),
GenericArg::Infer(ref inf) => self.hash_ty(&inf.to_ty()),
}
}