Rollup merge of #129386 - cjgillot:local-resolved-arg, r=compiler-errors

Use a LocalDefId in ResolvedArg.
This commit is contained in:
Matthias Krüger 2024-08-23 06:26:53 +02:00 committed by GitHub
commit 487b3d92cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 58 additions and 55 deletions

View file

@ -529,7 +529,7 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
match tcx.named_bound_var(hir_id) {
Some(ResolvedArg::EarlyBound(def_id)) => {
expected_captures.insert(def_id);
expected_captures.insert(def_id.to_def_id());
// Make sure we allow capturing these lifetimes through `Self` and
// `T::Assoc` projection syntax, too. These will occur when we only
@ -538,7 +538,7 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe
// feature -- see <https://github.com/rust-lang/rust/pull/115659>.
if let DefKind::LifetimeParam = tcx.def_kind(def_id)
&& let Some(def_id) = tcx
.map_opaque_lifetime_to_parent_lifetime(def_id.expect_local())
.map_opaque_lifetime_to_parent_lifetime(def_id)
.opt_param_def_id(tcx, tcx.parent(opaque_def_id.to_def_id()))
{
shadowed_captures.insert(def_id);

View file

@ -13,7 +13,6 @@ use rustc_ast::visit::walk_list;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirId, HirIdMap, LifetimeName, Node};
use rustc_macros::extension;
@ -22,7 +21,7 @@ use rustc_middle::middle::resolve_bound_vars::*;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor};
use rustc_middle::{bug, span_bug};
use rustc_span::def_id::DefId;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
@ -32,7 +31,7 @@ use crate::errors;
impl ResolvedArg {
fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
debug!("ResolvedArg::early: def_id={:?}", param.def_id);
(param.def_id, ResolvedArg::EarlyBound(param.def_id.to_def_id()))
(param.def_id, ResolvedArg::EarlyBound(param.def_id))
}
fn late(idx: u32, param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) {
@ -41,10 +40,10 @@ impl ResolvedArg {
"ResolvedArg::late: idx={:?}, param={:?} depth={:?} def_id={:?}",
idx, param, depth, param.def_id,
);
(param.def_id, ResolvedArg::LateBound(depth, idx, param.def_id.to_def_id()))
(param.def_id, ResolvedArg::LateBound(depth, idx, param.def_id))
}
fn id(&self) -> Option<DefId> {
fn id(&self) -> Option<LocalDefId> {
match *self {
ResolvedArg::StaticLifetime | ResolvedArg::Error(_) => None,
@ -288,13 +287,14 @@ fn late_arg_as_bound_arg<'tcx>(
) -> ty::BoundVariableKind {
match arg {
ResolvedArg::LateBound(_, _, def_id) => {
let name = tcx.hir().name(tcx.local_def_id_to_hir_id(def_id.expect_local()));
let def_id = def_id.to_def_id();
let name = tcx.item_name(def_id);
match param.kind {
GenericParamKind::Lifetime { .. } => {
ty::BoundVariableKind::Region(ty::BrNamed(*def_id, name))
ty::BoundVariableKind::Region(ty::BrNamed(def_id, name))
}
GenericParamKind::Type { .. } => {
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(*def_id, name))
ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id, name))
}
GenericParamKind::Const { .. } => ty::BoundVariableKind::Const,
}
@ -717,7 +717,6 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
// In the future, this should be fixed and this error should be removed.
let def = self.map.defs.get(&lifetime.hir_id).copied();
let Some(ResolvedArg::LateBound(_, _, lifetime_def_id)) = def else { continue };
let Some(lifetime_def_id) = lifetime_def_id.as_local() else { continue };
let lifetime_hir_id = self.tcx.local_def_id_to_hir_id(lifetime_def_id);
let bad_place = match self.tcx.hir_node(self.tcx.parent_hir_id(lifetime_hir_id))
@ -1150,7 +1149,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
.param_def_id_to_index(self.tcx, region_def_id.to_def_id())
.is_some()
{
break Some(ResolvedArg::EarlyBound(region_def_id.to_def_id()));
break Some(ResolvedArg::EarlyBound(region_def_id));
}
break None;
}
@ -1259,7 +1258,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
kind => span_bug!(
use_span,
"did not expect to resolve lifetime to {}",
kind.descr(param_def_id)
kind.descr(param_def_id.to_def_id())
),
};
def = ResolvedArg::Error(guar);
@ -1277,10 +1276,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
kind: hir::ImplItemKind::Fn(..),
..
}) => {
def = ResolvedArg::Free(owner_id.to_def_id(), def.id().unwrap());
def = ResolvedArg::Free(owner_id.def_id, def.id().unwrap());
}
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(closure), .. }) => {
def = ResolvedArg::Free(closure.def_id.to_def_id(), def.id().unwrap());
def = ResolvedArg::Free(closure.def_id, def.id().unwrap());
}
_ => {}
}
@ -1351,7 +1350,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
.param_def_id_to_index(self.tcx, param_def_id.to_def_id())
.is_some()
{
break Some(ResolvedArg::EarlyBound(param_def_id.to_def_id()));
break Some(ResolvedArg::EarlyBound(param_def_id));
}
break None;
}

View file

@ -296,25 +296,29 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Some(rbv::ResolvedArg::StaticLifetime) => tcx.lifetimes.re_static,
Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => {
let name = lifetime_name(def_id.expect_local());
let name = lifetime_name(def_id);
let br = ty::BoundRegion {
var: ty::BoundVar::from_u32(index),
kind: ty::BrNamed(def_id, name),
kind: ty::BrNamed(def_id.to_def_id(), name),
};
ty::Region::new_bound(tcx, debruijn, br)
}
Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
let name = tcx.hir().ty_param_name(def_id.expect_local());
let item_def_id = tcx.hir().ty_param_owner(def_id.expect_local());
let name = tcx.hir().ty_param_name(def_id);
let item_def_id = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id];
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
ty::Region::new_early_param(tcx, ty::EarlyParamRegion { index, name })
}
Some(rbv::ResolvedArg::Free(scope, id)) => {
let name = lifetime_name(id.expect_local());
ty::Region::new_late_param(tcx, scope, ty::BrNamed(id, name))
let name = lifetime_name(id);
ty::Region::new_late_param(
tcx,
scope.to_def_id(),
ty::BrNamed(id.to_def_id(), name),
)
// (*) -- not late-bound, won't change
}
@ -1953,15 +1957,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let tcx = self.tcx();
match tcx.named_bound_var(hir_id) {
Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => {
let name = tcx.item_name(def_id);
let name = tcx.item_name(def_id.to_def_id());
let br = ty::BoundTy {
var: ty::BoundVar::from_u32(index),
kind: ty::BoundTyKind::Param(def_id, name),
kind: ty::BoundTyKind::Param(def_id.to_def_id(), name),
};
Ty::new_bound(tcx, debruijn, br)
}
Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
let def_id = def_id.expect_local();
let item_def_id = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
@ -1982,10 +1985,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Some(rbv::ResolvedArg::EarlyBound(def_id)) => {
// Find the name and index of the const parameter by indexing the generics of
// the parent item and construct a `ParamConst`.
let item_def_id = tcx.parent(def_id);
let item_def_id = tcx.local_parent(def_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id];
let name = tcx.item_name(def_id);
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
let name = tcx.item_name(def_id.to_def_id());
ty::Const::new_param(tcx, ty::ParamConst::new(index, name))
}
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {