Skip to content

Commit 1bb8d27

Browse files
committed
Auto merge of rust-lang#101887 - nnethercote:shrink-Res, r=spastorino
Shrink `hir::def::Res` r? `@spastorino`
2 parents 9c56d9d + f07d4ef commit 1bb8d27

File tree

28 files changed

+225
-186
lines changed

28 files changed

+225
-186
lines changed

compiler/rustc_hir/src/def.rs

+65-50
Original file line numberDiff line numberDiff line change
@@ -314,89 +314,95 @@ pub enum Res<Id = hir::HirId> {
314314
/// **Belongs to the type namespace.**
315315
PrimTy(hir::PrimTy),
316316

317-
/// The `Self` type, optionally with the [`DefId`] of the trait it belongs to and
318-
/// optionally with the [`DefId`] of the item introducing the `Self` type alias.
317+
/// The `Self` type, as used within a trait.
318+
///
319+
/// **Belongs to the type namespace.**
320+
///
321+
/// See the examples on [`Res::SelfTyAlias`] for details.
322+
SelfTyParam {
323+
/// The trait this `Self` is a generic parameter for.
324+
trait_: DefId,
325+
},
326+
327+
/// The `Self` type, as used somewhere other than within a trait.
319328
///
320329
/// **Belongs to the type namespace.**
321330
///
322331
/// Examples:
323332
/// ```
324-
/// struct Bar(Box<Self>);
325-
/// // `Res::SelfTy { trait_: None, alias_of: Some(Bar) }`
333+
/// struct Bar(Box<Self>); // SelfTyAlias
326334
///
327335
/// trait Foo {
328-
/// fn foo() -> Box<Self>;
329-
/// // `Res::SelfTy { trait_: Some(Foo), alias_of: None }`
336+
/// fn foo() -> Box<Self>; // SelfTyParam
330337
/// }
331338
///
332339
/// impl Bar {
333340
/// fn blah() {
334-
/// let _: Self;
335-
/// // `Res::SelfTy { trait_: None, alias_of: Some(::{impl#0}) }`
341+
/// let _: Self; // SelfTyAlias
336342
/// }
337343
/// }
338344
///
339345
/// impl Foo for Bar {
340-
/// fn foo() -> Box<Self> {
341-
/// // `Res::SelfTy { trait_: Some(Foo), alias_of: Some(::{impl#1}) }`
342-
/// let _: Self;
343-
/// // `Res::SelfTy { trait_: Some(Foo), alias_of: Some(::{impl#1}) }`
346+
/// fn foo() -> Box<Self> { // SelfTyAlias
347+
/// let _: Self; // SelfTyAlias
344348
///
345349
/// todo!()
346350
/// }
347351
/// }
348352
/// ```
349-
///
350353
/// *See also [`Res::SelfCtor`].*
351354
///
352-
/// -----
353-
///
354-
/// HACK(min_const_generics): self types also have an optional requirement to **not** mention
355-
/// any generic parameters to allow the following with `min_const_generics`:
356-
/// ```
357-
/// # struct Foo;
358-
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
359-
///
360-
/// struct Bar([u8; baz::<Self>()]);
361-
/// const fn baz<T>() -> usize { 10 }
362-
/// ```
363-
/// We do however allow `Self` in repeat expression even if it is generic to not break code
364-
/// which already works on stable while causing the `const_evaluatable_unchecked` future compat
365-
/// lint:
366-
/// ```
367-
/// fn foo<T>() {
368-
/// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
369-
/// }
370-
/// ```
371-
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
372-
SelfTy {
373-
/// The trait this `Self` is a generic arg for.
374-
trait_: Option<DefId>,
355+
SelfTyAlias {
375356
/// The item introducing the `Self` type alias. Can be used in the `type_of` query
376-
/// to get the underlying type. Additionally whether the `Self` type is disallowed
377-
/// from mentioning generics (i.e. when used in an anonymous constant).
378-
alias_to: Option<(DefId, bool)>,
379-
},
357+
/// to get the underlying type.
358+
alias_to: DefId,
380359

381-
/// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
382-
///
383-
/// **Belongs to the type namespace.**
384-
ToolMod,
360+
/// Whether the `Self` type is disallowed from mentioning generics (i.e. when used in an
361+
/// anonymous constant).
362+
///
363+
/// HACK(min_const_generics): self types also have an optional requirement to **not**
364+
/// mention any generic parameters to allow the following with `min_const_generics`:
365+
/// ```
366+
/// # struct Foo;
367+
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
368+
///
369+
/// struct Bar([u8; baz::<Self>()]);
370+
/// const fn baz<T>() -> usize { 10 }
371+
/// ```
372+
/// We do however allow `Self` in repeat expression even if it is generic to not break code
373+
/// which already works on stable while causing the `const_evaluatable_unchecked` future
374+
/// compat lint:
375+
/// ```
376+
/// fn foo<T>() {
377+
/// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
378+
/// }
379+
/// ```
380+
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
381+
forbid_generic: bool,
382+
383+
/// Is this within an `impl Foo for bar`?
384+
is_trait_impl: bool,
385+
},
385386

386387
// Value namespace
387388
/// The `Self` constructor, along with the [`DefId`]
388389
/// of the impl it is associated with.
389390
///
390391
/// **Belongs to the value namespace.**
391392
///
392-
/// *See also [`Res::SelfTy`].*
393+
/// *See also [`Res::SelfTyParam`] and [`Res::SelfTyAlias`].*
393394
SelfCtor(DefId),
394395

395396
/// A local variable or function parameter.
396397
///
397398
/// **Belongs to the value namespace.**
398399
Local(Id),
399400

401+
/// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
402+
///
403+
/// **Belongs to the type namespace.**
404+
ToolMod,
405+
400406
// Macro namespace
401407
/// An attribute that is *not* implemented via macro.
402408
/// E.g., `#[inline]` and `#[rustfmt::skip]`, which are essentially directives,
@@ -606,7 +612,8 @@ impl<Id> Res<Id> {
606612

607613
Res::Local(..)
608614
| Res::PrimTy(..)
609-
| Res::SelfTy { .. }
615+
| Res::SelfTyParam { .. }
616+
| Res::SelfTyAlias { .. }
610617
| Res::SelfCtor(..)
611618
| Res::ToolMod
612619
| Res::NonMacroAttr(..)
@@ -629,7 +636,7 @@ impl<Id> Res<Id> {
629636
Res::SelfCtor(..) => "self constructor",
630637
Res::PrimTy(..) => "builtin type",
631638
Res::Local(..) => "local variable",
632-
Res::SelfTy { .. } => "self type",
639+
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => "self type",
633640
Res::ToolMod => "tool module",
634641
Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
635642
Res::Err => "unresolved item",
@@ -652,7 +659,10 @@ impl<Id> Res<Id> {
652659
Res::SelfCtor(id) => Res::SelfCtor(id),
653660
Res::PrimTy(id) => Res::PrimTy(id),
654661
Res::Local(id) => Res::Local(map(id)),
655-
Res::SelfTy { trait_, alias_to } => Res::SelfTy { trait_, alias_to },
662+
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
663+
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
664+
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
665+
}
656666
Res::ToolMod => Res::ToolMod,
657667
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
658668
Res::Err => Res::Err,
@@ -665,7 +675,10 @@ impl<Id> Res<Id> {
665675
Res::SelfCtor(id) => Res::SelfCtor(id),
666676
Res::PrimTy(id) => Res::PrimTy(id),
667677
Res::Local(id) => Res::Local(map(id)?),
668-
Res::SelfTy { trait_, alias_to } => Res::SelfTy { trait_, alias_to },
678+
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
679+
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
680+
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
681+
}
669682
Res::ToolMod => Res::ToolMod,
670683
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
671684
Res::Err => Res::Err,
@@ -692,7 +705,9 @@ impl<Id> Res<Id> {
692705
pub fn ns(&self) -> Option<Namespace> {
693706
match self {
694707
Res::Def(kind, ..) => kind.ns(),
695-
Res::PrimTy(..) | Res::SelfTy { .. } | Res::ToolMod => Some(Namespace::TypeNS),
708+
Res::PrimTy(..) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::ToolMod => {
709+
Some(Namespace::TypeNS)
710+
}
696711
Res::SelfCtor(..) | Res::Local(..) => Some(Namespace::ValueNS),
697712
Res::NonMacroAttr(..) => Some(Namespace::MacroNS),
698713
Res::Err => None,

compiler/rustc_hir/src/hir.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2404,8 +2404,9 @@ impl<'hir> Ty<'hir> {
24042404
return None;
24052405
};
24062406
match path.res {
2407-
Res::Def(DefKind::TyParam, def_id)
2408-
| Res::SelfTy { trait_: Some(def_id), alias_to: None } => Some((def_id, segment.ident)),
2407+
Res::Def(DefKind::TyParam, def_id) | Res::SelfTyParam { trait_: def_id } => {
2408+
Some((def_id, segment.ident))
2409+
}
24092410
_ => None,
24102411
}
24112412
}
@@ -3533,9 +3534,10 @@ mod size_asserts {
35333534
static_assert_size!(Param<'_>, 32);
35343535
static_assert_size!(Pat<'_>, 72);
35353536
static_assert_size!(PatKind<'_>, 48);
3536-
static_assert_size!(Path<'_>, 48);
3537-
static_assert_size!(PathSegment<'_>, 56);
3537+
static_assert_size!(Path<'_>, 40);
3538+
static_assert_size!(PathSegment<'_>, 48);
35383539
static_assert_size!(QPath<'_>, 24);
3540+
static_assert_size!(Res, 12);
35393541
static_assert_size!(Stmt<'_>, 32);
35403542
static_assert_size!(StmtKind<'_>, 16);
35413543
static_assert_size!(TraitItem<'_>, 88);

compiler/rustc_hir_analysis/src/astconv/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19021902
// Find the type of the associated item, and the trait where the associated
19031903
// item is declared.
19041904
let bound = match (&qself_ty.kind(), qself_res) {
1905-
(_, Res::SelfTy { trait_: Some(_), alias_to: Some((impl_def_id, _)) }) => {
1905+
(_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => {
19061906
// `Self` in an impl of a trait -- we have a concrete self type and a
19071907
// trait reference.
19081908
let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) else {
@@ -1921,8 +1921,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19211921
}
19221922
(
19231923
&ty::Param(_),
1924-
Res::SelfTy { trait_: Some(param_did), alias_to: None }
1925-
| Res::Def(DefKind::TyParam, param_did),
1924+
Res::SelfTyParam { trait_: param_did } | Res::Def(DefKind::TyParam, param_did),
19261925
) => self.find_bound_for_assoc_item(param_did.expect_local(), assoc_ident, span)?,
19271926
_ => {
19281927
let reported = if variant_resolution.is_some() {
@@ -2417,7 +2416,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24172416
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
24182417
tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id))
24192418
}
2420-
Res::SelfTy { trait_: Some(_), alias_to: None } => {
2419+
Res::SelfTyParam { .. } => {
24212420
// `Self` in trait or type alias.
24222421
assert_eq!(opt_self_ty, None);
24232422
self.prohibit_generics(path.segments.iter(), |err| {
@@ -2432,7 +2431,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24322431
});
24332432
tcx.types.self_param
24342433
}
2435-
Res::SelfTy { trait_: _, alias_to: Some((def_id, forbid_generic)) } => {
2434+
Res::SelfTyAlias { alias_to: def_id, forbid_generic, .. } => {
24362435
// `Self` in impl (we know the concrete type).
24372436
assert_eq!(opt_self_ty, None);
24382437
// Try to evaluate any array length constants.

compiler/rustc_hir_analysis/src/check/check.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,12 @@ pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
609609
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
610610
match arg.kind {
611611
hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
612-
[PathSegment { res: Res::SelfTy { trait_: _, alias_to: impl_ref }, .. }] => {
613-
let impl_ty_name =
614-
impl_ref.map(|(def_id, _)| self.tcx.def_path_str(def_id));
612+
[PathSegment { res: Res::SelfTyParam { .. }, .. }] => {
613+
let impl_ty_name = None;
614+
self.selftys.push((path.span, impl_ty_name));
615+
}
616+
[PathSegment { res: Res::SelfTyAlias { alias_to: def_id, .. }, .. }] => {
617+
let impl_ty_name = Some(self.tcx.def_path_str(*def_id));
615618
self.selftys.push((path.span, impl_ty_name));
616619
}
617620
_ => {}

compiler/rustc_hir_analysis/src/check/fn_ctxt/checks.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11991199
_ => bug!("unexpected type: {:?}", ty),
12001200
},
12011201
Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
1202-
| Res::SelfTy { .. } => match ty.kind() {
1202+
| Res::SelfTyParam { .. }
1203+
| Res::SelfTyAlias { .. } => match ty.kind() {
12031204
ty::Adt(adt, substs) if !adt.is_enum() => {
12041205
Some((adt.non_enum_variant(), adt.did(), substs))
12051206
}

compiler/rustc_hir_analysis/src/mem_categorization.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
560560
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), _)
561561
| Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _)
562562
| Res::SelfCtor(..)
563-
| Res::SelfTy { .. } => {
563+
| Res::SelfTyParam { .. }
564+
| Res::SelfTyAlias { .. } => {
564565
// Structs and Unions have only have one variant.
565566
Ok(VariantIdx::new(0))
566567
}

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
10211021
}
10221022
// There cannot be inference variables in the self type,
10231023
// so there's nothing for us to do here.
1024-
Res::SelfTy { .. } => {}
1024+
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {}
10251025
_ => warn!(
10261026
"unexpected path: def={:?} substs={:?} path={:?}",
10271027
def, substs, path,

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
156156
[segment]
157157
if matches!(
158158
segment.res,
159-
Res::SelfTy { trait_: _, alias_to: _ }
159+
Res::SelfTyParam { .. }
160+
| Res::SelfTyAlias { .. }
160161
| Res::Def(hir::def::DefKind::TyParam, _)
161162
) =>
162163
{

compiler/rustc_lint/src/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option<String> {
244244
}
245245
}
246246
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
247-
Res::SelfTy { trait_: None, alias_to: Some((did, _)) } => {
247+
Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => {
248248
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
249249
if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(adt.did())
250250
{

compiler/rustc_lint/src/pass_by_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
5656
let path_segment = path.segments.last().unwrap();
5757
return Some(format!("{}{}", name, gen_args(cx, path_segment)));
5858
}
59-
Res::SelfTy { trait_: None, alias_to: Some((did, _)) } => {
59+
Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => {
6060
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
6161
if cx.tcx.has_attr(adt.did(), sym::rustc_pass_by_value) {
6262
return Some(cx.tcx.def_path_str_with_substs(adt.did(), substs));

compiler/rustc_middle/src/ty/adt.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ impl<'tcx> AdtDef<'tcx> {
438438
| Res::Def(DefKind::Union, _)
439439
| Res::Def(DefKind::TyAlias, _)
440440
| Res::Def(DefKind::AssocTy, _)
441-
| Res::SelfTy { .. }
441+
| Res::SelfTyParam { .. }
442+
| Res::SelfTyAlias { .. }
442443
| Res::SelfCtor(..) => self.non_enum_variant(),
443444
_ => bug!("unexpected res {:?} in variant_of_res", res),
444445
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
433433
| DefKind::AssocTy,
434434
_,
435435
)
436-
| Res::SelfTy { .. }
436+
| Res::SelfTyParam { .. }
437+
| Res::SelfTyAlias { .. }
437438
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
438439
_ => {
439440
let pattern_error = match res {

compiler/rustc_passes/src/dead.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
102102
}
103103
}
104104
Res::Def(_, def_id) => self.check_def_id(def_id),
105-
Res::SelfTy { trait_: t, alias_to: i } => {
106-
if let Some(t) = t {
107-
self.check_def_id(t);
108-
}
109-
if let Some((i, _)) = i {
110-
self.check_def_id(i);
111-
}
112-
}
105+
Res::SelfTyParam { trait_: t } => self.check_def_id(t),
106+
Res::SelfTyAlias { alias_to: i, .. } => self.check_def_id(i),
113107
Res::ToolMod | Res::NonMacroAttr(..) | Res::Err => {}
114108
}
115109
}

compiler/rustc_privacy/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,9 @@ struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
14221422
impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
14231423
fn path_is_private_type(&self, path: &hir::Path<'_>) -> bool {
14241424
let did = match path.res {
1425-
Res::PrimTy(..) | Res::SelfTy { .. } | Res::Err => return false,
1425+
Res::PrimTy(..) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Err => {
1426+
return false;
1427+
}
14261428
res => res.def_id(),
14271429
};
14281430

compiler/rustc_resolve/src/build_reduced_graph.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
10101010
_,
10111011
)
10121012
| Res::Local(..)
1013-
| Res::SelfTy { .. }
1013+
| Res::SelfTyParam { .. }
1014+
| Res::SelfTyAlias { .. }
10141015
| Res::SelfCtor(..)
10151016
| Res::Err => bug!("unexpected resolution: {:?}", res),
10161017
}

0 commit comments

Comments
 (0)