1
Fork 0

Auto merge of #101887 - nnethercote:shrink-Res, r=spastorino

Shrink `hir::def::Res`

r? `@spastorino`
This commit is contained in:
bors 2022-09-29 22:45:24 +00:00
commit 1bb8d276c9
28 changed files with 225 additions and 186 deletions

View file

@ -1010,7 +1010,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
_,
)
| Res::Local(..)
| Res::SelfTy { .. }
| Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. }
| Res::SelfCtor(..)
| Res::Err => bug!("unexpected resolution: {:?}", res),
}

View file

@ -511,24 +511,18 @@ impl<'a> Resolver<'a> {
let sm = self.session.source_map();
let def_id = match outer_res {
Res::SelfTy { trait_: maybe_trait_defid, alias_to: maybe_impl_defid } => {
if let Some(impl_span) =
maybe_impl_defid.and_then(|(def_id, _)| self.opt_span(def_id))
{
Res::SelfTyParam { .. } => {
err.span_label(span, "can't use `Self` here");
return err;
}
Res::SelfTyAlias { alias_to: def_id, .. } => {
if let Some(impl_span) = self.opt_span(def_id) {
err.span_label(
reduce_impl_span_to_impl_keyword(sm, impl_span),
"`Self` type implicitly declared here, by this `impl`",
);
}
match (maybe_trait_defid, maybe_impl_defid) {
(Some(_), None) => {
err.span_label(span, "can't use `Self` here");
}
(_, Some(_)) => {
err.span_label(span, "use a type here instead");
}
(None, None) => bug!("`impl` without trait nor type?"),
}
err.span_label(span, "use a type here instead");
return err;
}
Res::Def(DefKind::TyParam, def_id) => {
@ -545,8 +539,9 @@ impl<'a> Resolver<'a> {
}
_ => {
bug!(
"GenericParamsFromOuterFunction should only be used with Res::SelfTy, \
DefKind::TyParam or DefKind::ConstParam"
"GenericParamsFromOuterFunction should only be used with \
Res::SelfTyParam, Res::SelfTyAlias, DefKind::TyParam or \
DefKind::ConstParam"
);
}
};

View file

@ -1162,7 +1162,7 @@ impl<'a> Resolver<'a> {
return Res::Err;
}
}
Res::Def(DefKind::TyParam, _) | Res::SelfTy { .. } => {
Res::Def(DefKind::TyParam, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {
for rib in ribs {
let has_generic_params: HasGenericParams = match rib.kind {
NormalRibKind
@ -1182,11 +1182,21 @@ impl<'a> Resolver<'a> {
if !(trivial == ConstantHasGenerics::Yes
|| features.generic_const_exprs)
{
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
// we can't easily tell if it's generic at this stage, so we instead remember
// this and then enforce the self type to be concrete later on.
if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res {
res = Res::SelfTy { trait_, alias_to: Some((def, true)) }
// HACK(min_const_generics): If we encounter `Self` in an anonymous
// constant we can't easily tell if it's generic at this stage, so
// we instead remember this and then enforce the self type to be
// concrete later on.
if let Res::SelfTyAlias {
alias_to: def,
forbid_generic: _,
is_trait_impl,
} = res
{
res = Res::SelfTyAlias {
alias_to: def,
forbid_generic: true,
is_trait_impl,
}
} else {
if let Some(span) = finalize {
self.report_error(

View file

@ -19,7 +19,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::DiagnosticId;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, PartialRes, PerNS};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{BindingAnnotation, PrimTy, TraitCandidate};
use rustc_middle::middle::resolve_lifetime::Set1;
use rustc_middle::ty::DefIdTree;
@ -414,7 +414,8 @@ impl<'a> PathSource<'a> {
| DefKind::ForeignTy,
_,
) | Res::PrimTy(..)
| Res::SelfTy { .. }
| Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. }
),
PathSource::Trait(AliasPossibility::No) => matches!(res, Res::Def(DefKind::Trait, _)),
PathSource::Trait(AliasPossibility::Maybe) => {
@ -448,7 +449,8 @@ impl<'a> PathSource<'a> {
| DefKind::TyAlias
| DefKind::AssocTy,
_,
) | Res::SelfTy { .. }
) | Res::SelfTyParam { .. }
| Res::SelfTyAlias { .. }
),
PathSource::TraitItem(ns) => match res {
Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) if ns == ValueNS => true,
@ -1929,7 +1931,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
TyKind::ImplicitSelf => true,
TyKind::Path(None, _) => {
let path_res = self.r.partial_res_map[&ty.id].base_res();
if let Res::SelfTy { .. } = path_res {
if let Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } = path_res {
return true;
}
Some(path_res) == self.impl_self
@ -2050,7 +2052,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|this| {
let item_def_id = this.r.local_def_id(item.id).to_def_id();
this.with_self_rib(
Res::SelfTy { trait_: None, alias_to: Some((item_def_id, false)) },
Res::SelfTyAlias {
alias_to: item_def_id,
forbid_generic: false,
is_trait_impl: false,
},
|this| {
visit::walk_item(this, item);
},
@ -2164,14 +2170,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
|this| {
let local_def_id = this.r.local_def_id(item.id).to_def_id();
this.with_self_rib(
Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
|this| {
this.visit_generics(generics);
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
this.resolve_trait_items(items);
},
);
this.with_self_rib(Res::SelfTyParam { trait_: local_def_id }, |this| {
this.visit_generics(generics);
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
this.resolve_trait_items(items);
});
},
);
}
@ -2188,13 +2191,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
|this| {
let local_def_id = this.r.local_def_id(item.id).to_def_id();
this.with_self_rib(
Res::SelfTy { trait_: Some(local_def_id), alias_to: None },
|this| {
this.visit_generics(generics);
walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
},
);
this.with_self_rib(Res::SelfTyParam { trait_: local_def_id }, |this| {
this.visit_generics(generics);
walk_list!(this, visit_param_bound, bounds, BoundKind::Bound);
});
},
);
}
@ -2576,7 +2576,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
},
|this| {
// Dummy self type for better errors if `Self` is used in the trait path.
this.with_self_rib(Res::SelfTy { trait_: None, alias_to: None }, |this| {
this.with_self_rib(Res::SelfTyParam { trait_: LOCAL_CRATE.as_def_id() }, |this| {
this.with_lifetime_rib(
LifetimeRibKind::AnonymousCreateParameter {
binder: item_id,
@ -2600,9 +2600,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
let item_def_id = item_def_id.to_def_id();
let res = Res::SelfTy {
trait_: trait_id,
alias_to: Some((item_def_id, false)),
let res = Res::SelfTyAlias {
alias_to: item_def_id,
forbid_generic: false,
is_trait_impl: trait_id.is_some()
};
this.with_self_rib(res, |this| {
if let Some(trait_ref) = opt_trait_reference.as_ref() {

View file

@ -1449,7 +1449,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
Applicability::HasPlaceholders,
);
}
(Res::SelfTy { .. }, _) if ns == ValueNS => {
(Res::SelfTyParam { .. } | Res::SelfTyAlias { .. }, _) if ns == ValueNS => {
err.span_label(span, fallback_label);
err.note("can't use `Self` as a constructor, you must use the implemented struct");
}