Skip to content

Commit b321742

Browse files
committed
Auto merge of rust-lang#93938 - BoxyUwU:fix_res_self_ty, r=lcnr
Make `Res::SelfTy` a struct variant and update docs I found pattern matching on a `(Option<DefId>, Option<(DefId, bool)>)` to not be super readable, additionally the doc comments on the types in a tuple variant aren't visible anywhere at use sites as far as I can tell (using rust analyzer + vscode) The docs incorrectly assumed that the `DefId` in `Option<(DefId, bool)>` would only ever be for an impl item and I also found the code examples to be somewhat unclear about which `DefId` was being talked about. r? `@lcnr` since you reviewed the last PR changing these docs
2 parents 902e590 + 48a79bc commit b321742

File tree

27 files changed

+102
-83
lines changed

27 files changed

+102
-83
lines changed

compiler/rustc_hir/src/def.rs

+41-33
Original file line numberDiff line numberDiff line change
@@ -266,59 +266,67 @@ pub enum Res<Id = hir::HirId> {
266266
///
267267
/// **Belongs to the type namespace.**
268268
PrimTy(hir::PrimTy),
269-
/// The `Self` type, optionally with the trait it is associated with
270-
/// and optionally with the [`DefId`] of the impl it is associated with.
269+
/// The `Self` type, optionally with the [`DefId`] of the trait it belongs to and
270+
/// optionally with the [`DefId`] of the item introducing the `Self` type alias.
271271
///
272272
/// **Belongs to the type namespace.**
273273
///
274-
/// For example, the `Self` in
275-
///
274+
/// Examples:
276275
/// ```
276+
/// struct Bar(Box<Self>);
277+
/// // `Res::SelfTy { trait_: None, alias_of: Some(Bar) }`
278+
///
277279
/// trait Foo {
278280
/// fn foo() -> Box<Self>;
281+
/// // `Res::SelfTy { trait_: Some(Foo), alias_of: None }`
279282
/// }
280-
/// ```
281-
///
282-
/// would have the [`DefId`] of `Foo` associated with it. The `Self` in
283-
///
284-
/// ```
285-
/// struct Bar;
286283
///
287284
/// impl Bar {
288-
/// fn new() -> Self { Bar }
285+
/// fn blah() {
286+
/// let _: Self;
287+
/// // `Res::SelfTy { trait_: None, alias_of: Some(::{impl#0}) }`
288+
/// }
289289
/// }
290-
/// ```
291-
///
292-
/// would have the [`DefId`] of the impl associated with it. Finally, the `Self` in
293290
///
294-
/// ```
295291
/// impl Foo for Bar {
296-
/// fn foo() -> Box<Self> { Box::new(Bar) }
292+
/// fn foo() -> Box<Self> {
293+
/// // `Res::SelfTy { trait_: Some(Foo), alias_of: Some(::{impl#1}) }`
294+
/// let _: Self;
295+
/// // `Res::SelfTy { trait_: Some(Foo), alias_of: Some(::{impl#1}) }`
296+
///
297+
/// todo!()
298+
/// }
297299
/// }
298300
/// ```
299301
///
300-
/// would have both the [`DefId`] of `Foo` and the [`DefId`] of the impl
301-
/// associated with it.
302-
///
303302
/// *See also [`Res::SelfCtor`].*
304303
///
305304
/// -----
306305
///
307-
/// HACK(min_const_generics): impl self types also have an optional requirement to **not** mention
306+
/// HACK(min_const_generics): self types also have an optional requirement to **not** mention
308307
/// any generic parameters to allow the following with `min_const_generics`:
309308
/// ```
310309
/// impl Foo { fn test() -> [u8; std::mem::size_of::<Self>()] { todo!() } }
310+
///
311+
/// struct Bar([u8; baz::<Self>()]);
312+
/// const fn baz<T>() -> usize { 10 }
311313
/// ```
312314
/// We do however allow `Self` in repeat expression even if it is generic to not break code
313-
/// which already works on stable while causing the `const_evaluatable_unchecked` future compat lint.
314-
///
315-
/// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
316-
SelfTy(
317-
/// Optionally, the trait associated with this `Self` type.
318-
Option<DefId>,
319-
/// Optionally, the impl associated with this `Self` type.
320-
Option<(DefId, bool)>,
321-
),
315+
/// which already works on stable while causing the `const_evaluatable_unchecked` future compat lint:
316+
/// ```
317+
/// fn foo<T>() {
318+
/// let _bar = [1_u8; std::mem::size_of::<*mut T>()];
319+
/// }
320+
/// ```
321+
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
322+
SelfTy {
323+
/// The trait this `Self` is a generic arg for.
324+
trait_: Option<DefId>,
325+
/// The item introducing the `Self` type alias. Can be used in the `type_of` query
326+
/// to get the underlying type. Additionally whether the `Self` type is disallowed
327+
/// from mentioning generics (i.e. when used in an anonymous constant).
328+
alias_to: Option<(DefId, bool)>,
329+
},
322330
/// A tool attribute module; e.g., the `rustfmt` in `#[rustfmt::skip]`.
323331
///
324332
/// **Belongs to the type namespace.**
@@ -550,7 +558,7 @@ impl<Id> Res<Id> {
550558

551559
Res::Local(..)
552560
| Res::PrimTy(..)
553-
| Res::SelfTy(..)
561+
| Res::SelfTy { .. }
554562
| Res::SelfCtor(..)
555563
| Res::ToolMod
556564
| Res::NonMacroAttr(..)
@@ -573,7 +581,7 @@ impl<Id> Res<Id> {
573581
Res::SelfCtor(..) => "self constructor",
574582
Res::PrimTy(..) => "builtin type",
575583
Res::Local(..) => "local variable",
576-
Res::SelfTy(..) => "self type",
584+
Res::SelfTy { .. } => "self type",
577585
Res::ToolMod => "tool module",
578586
Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
579587
Res::Err => "unresolved item",
@@ -596,7 +604,7 @@ impl<Id> Res<Id> {
596604
Res::SelfCtor(id) => Res::SelfCtor(id),
597605
Res::PrimTy(id) => Res::PrimTy(id),
598606
Res::Local(id) => Res::Local(map(id)),
599-
Res::SelfTy(a, b) => Res::SelfTy(a, b),
607+
Res::SelfTy { trait_, alias_to } => Res::SelfTy { trait_, alias_to },
600608
Res::ToolMod => Res::ToolMod,
601609
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
602610
Res::Err => Res::Err,
@@ -620,7 +628,7 @@ impl<Id> Res<Id> {
620628
pub fn ns(&self) -> Option<Namespace> {
621629
match self {
622630
Res::Def(kind, ..) => kind.ns(),
623-
Res::PrimTy(..) | Res::SelfTy(..) | Res::ToolMod => Some(Namespace::TypeNS),
631+
Res::PrimTy(..) | Res::SelfTy { .. } | Res::ToolMod => Some(Namespace::TypeNS),
624632
Res::SelfCtor(..) | Res::Local(..) => Some(Namespace::ValueNS),
625633
Res::NonMacroAttr(..) => Some(Namespace::MacroNS),
626634
Res::Err => None,

compiler/rustc_hir/src/hir.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,8 @@ impl<'hir> WhereBoundPredicate<'hir> {
640640
_ => return false,
641641
};
642642
match path.res {
643-
Res::Def(DefKind::TyParam, def_id) | Res::SelfTy(Some(def_id), None) => {
644-
def_id == param_def_id
645-
}
643+
Res::Def(DefKind::TyParam, def_id)
644+
| Res::SelfTy { trait_: Some(def_id), alias_to: None } => def_id == param_def_id,
646645
_ => false,
647646
}
648647
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ impl<'tcx> Visitor<'tcx> for TypeParamSpanVisitor<'tcx> {
203203
.map(|res| {
204204
matches!(
205205
res,
206-
Res::SelfTy(_, _) | Res::Def(hir::def::DefKind::TyParam, _)
206+
Res::SelfTy { trait_: _, alias_to: _ }
207+
| Res::Def(hir::def::DefKind::TyParam, _)
207208
)
208209
})
209210
.unwrap_or(false) =>

compiler/rustc_lint/src/internal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, ty: &Ty<'_>) -> Option<String> {
202202
}
203203
}
204204
// Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait.
205-
Res::SelfTy(None, Some((did, _))) => {
205+
Res::SelfTy { trait_: None, alias_to: Some((did, _)) } => {
206206
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
207207
if let Some(name @ (sym::Ty | sym::TyCtxt)) =
208208
cx.tcx.get_diagnostic_name(adt.did)

compiler/rustc_lint/src/pass_by_value.rs

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

compiler/rustc_middle/src/ty/adt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ impl<'tcx> AdtDef {
395395
| Res::Def(DefKind::Union, _)
396396
| Res::Def(DefKind::TyAlias, _)
397397
| Res::Def(DefKind::AssocTy, _)
398-
| Res::SelfTy(..)
398+
| Res::SelfTy { .. }
399399
| Res::SelfCtor(..) => self.non_enum_variant(),
400400
_ => bug!("unexpected res {:?} in variant_of_res", res),
401401
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
417417
| DefKind::AssocTy,
418418
_,
419419
)
420-
| Res::SelfTy(..)
420+
| Res::SelfTy { .. }
421421
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
422422
_ => {
423423
let pattern_error = match res {

compiler/rustc_passes/src/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
104104
self.check_def_id(variant_id);
105105
}
106106
}
107-
Res::SelfTy(t, i) => {
107+
Res::SelfTy { trait_: t, alias_to: i } => {
108108
if let Some(t) = t {
109109
self.check_def_id(t);
110110
}

compiler/rustc_privacy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ struct ObsoleteCheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
13501350
impl<'a, 'tcx> ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
13511351
fn path_is_private_type(&self, path: &hir::Path<'_>) -> bool {
13521352
let did = match path.res {
1353-
Res::PrimTy(..) | Res::SelfTy(..) | Res::Err => return false,
1353+
Res::PrimTy(..) | Res::SelfTy { .. } | Res::Err => return false,
13541354
res => res.def_id(),
13551355
};
13561356

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
991991
_,
992992
)
993993
| Res::Local(..)
994-
| Res::SelfTy(..)
994+
| Res::SelfTy { .. }
995995
| Res::SelfCtor(..)
996996
| Res::Err => bug!("unexpected resolution: {:?}", res),
997997
}

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a> Resolver<'a> {
123123

124124
let sm = self.session.source_map();
125125
match outer_res {
126-
Res::SelfTy(maybe_trait_defid, maybe_impl_defid) => {
126+
Res::SelfTy { trait_: maybe_trait_defid, alias_to: maybe_impl_defid } => {
127127
if let Some(impl_span) =
128128
maybe_impl_defid.and_then(|(def_id, _)| self.opt_span(def_id))
129129
{

compiler/rustc_resolve/src/late.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl<'a> PathSource<'a> {
289289
| DefKind::ForeignTy,
290290
_,
291291
) | Res::PrimTy(..)
292-
| Res::SelfTy(..)
292+
| Res::SelfTy { .. }
293293
),
294294
PathSource::Trait(AliasPossibility::No) => matches!(res, Res::Def(DefKind::Trait, _)),
295295
PathSource::Trait(AliasPossibility::Maybe) => {
@@ -326,7 +326,7 @@ impl<'a> PathSource<'a> {
326326
| DefKind::TyAlias
327327
| DefKind::AssocTy,
328328
_,
329-
) | Res::SelfTy(..)
329+
) | Res::SelfTy { .. }
330330
),
331331
PathSource::TraitItem(ns) => match res {
332332
Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) if ns == ValueNS => true,
@@ -911,9 +911,12 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
911911
self.with_current_self_item(item, |this| {
912912
this.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
913913
let item_def_id = this.r.local_def_id(item.id).to_def_id();
914-
this.with_self_rib(Res::SelfTy(None, Some((item_def_id, false))), |this| {
915-
visit::walk_item(this, item);
916-
});
914+
this.with_self_rib(
915+
Res::SelfTy { trait_: None, alias_to: Some((item_def_id, false)) },
916+
|this| {
917+
visit::walk_item(this, item);
918+
},
919+
);
917920
});
918921
});
919922
}
@@ -999,8 +1002,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
9991002
self.compute_num_lifetime_params(item.id, generics);
10001003
// Create a new rib for the trait-wide type parameters.
10011004
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
1002-
let local_def_id = this.r.local_def_id(item.id).to_def_id();
1003-
this.with_self_rib(Res::SelfTy(Some(local_def_id), None), |this| {
1005+
let def = this.r.local_def_id(item.id).to_def_id();
1006+
this.with_self_rib(Res::SelfTy { trait_: Some(def), alias_to: None }, |this| {
10041007
this.visit_generics(generics);
10051008
walk_list!(this, visit_param_bound, bounds);
10061009

@@ -1051,8 +1054,8 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
10511054
self.compute_num_lifetime_params(item.id, generics);
10521055
// Create a new rib for the trait-wide type parameters.
10531056
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
1054-
let local_def_id = this.r.local_def_id(item.id).to_def_id();
1055-
this.with_self_rib(Res::SelfTy(Some(local_def_id), None), |this| {
1057+
let def = this.r.local_def_id(item.id).to_def_id();
1058+
this.with_self_rib(Res::SelfTy { trait_: Some(def), alias_to: None }, |this| {
10561059
this.visit_generics(generics);
10571060
walk_list!(this, visit_param_bound, bounds);
10581061
});
@@ -1296,7 +1299,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12961299
// If applicable, create a rib for the type parameters.
12971300
self.with_generic_param_rib(generics, ItemRibKind(HasGenericParams::Yes), |this| {
12981301
// Dummy self type for better errors if `Self` is used in the trait path.
1299-
this.with_self_rib(Res::SelfTy(None, None), |this| {
1302+
this.with_self_rib(Res::SelfTy { trait_: None, alias_to: None }, |this| {
13001303
// Resolve the trait reference, if necessary.
13011304
this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| {
13021305
let item_def_id = this.r.local_def_id(item_id);
@@ -1307,7 +1310,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
13071310
}
13081311

13091312
let item_def_id = item_def_id.to_def_id();
1310-
this.with_self_rib(Res::SelfTy(trait_id, Some((item_def_id, false))), |this| {
1313+
let res =
1314+
Res::SelfTy { trait_: trait_id, alias_to: Some((item_def_id, false)) };
1315+
this.with_self_rib(res, |this| {
13111316
if let Some(trait_ref) = opt_trait_reference.as_ref() {
13121317
// Resolve type arguments in the trait path.
13131318
visit::walk_trait_ref(this, trait_ref);

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
11891189
Applicability::HasPlaceholders,
11901190
);
11911191
}
1192-
(Res::SelfTy(..), _) if ns == ValueNS => {
1192+
(Res::SelfTy { .. }, _) if ns == ValueNS => {
11931193
err.span_label(span, fallback_label);
11941194
err.note("can't use `Self` as a constructor, you must use the implemented struct");
11951195
}

compiler/rustc_resolve/src/late/lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2811,7 +2811,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
28112811
// Look for `self: &'a Self` - also desugared from `&'a self`,
28122812
// and if that matches, use it for elision and return early.
28132813
fn is_self_ty(&self, res: Res) -> bool {
2814-
if let Res::SelfTy(..) = res {
2814+
if let Res::SelfTy { .. } = res {
28152815
return true;
28162816
}
28172817

compiler/rustc_resolve/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ impl<'a> Resolver<'a> {
27842784
return Res::Err;
27852785
}
27862786
}
2787-
Res::Def(DefKind::TyParam, _) | Res::SelfTy(..) => {
2787+
Res::Def(DefKind::TyParam, _) | Res::SelfTy { .. } => {
27882788
for rib in ribs {
27892789
let has_generic_params: HasGenericParams = match rib.kind {
27902790
NormalRibKind
@@ -2804,8 +2804,8 @@ impl<'a> Resolver<'a> {
28042804
// HACK(min_const_generics): If we encounter `Self` in an anonymous constant
28052805
// we can't easily tell if it's generic at this stage, so we instead remember
28062806
// this and then enforce the self type to be concrete later on.
2807-
if let Res::SelfTy(trait_def, Some((impl_def, _))) = res {
2808-
res = Res::SelfTy(trait_def, Some((impl_def, true)));
2807+
if let Res::SelfTy { trait_, alias_to: Some((def, _)) } = res {
2808+
res = Res::SelfTy { trait_, alias_to: Some((def, true)) }
28092809
} else {
28102810
if record_used {
28112811
self.report_error(

compiler/rustc_save_analysis/src/dump_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ impl<'tcx> DumpVisitor<'tcx> {
921921
| HirDefKind::AssocTy,
922922
_,
923923
)
924-
| Res::SelfTy(..) => {
924+
| Res::SelfTy { .. } => {
925925
self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident));
926926
}
927927
def => {

compiler/rustc_save_analysis/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ impl<'tcx> SaveContext<'tcx> {
749749
_,
750750
)
751751
| Res::PrimTy(..)
752-
| Res::SelfTy(..)
752+
| Res::SelfTy { .. }
753753
| Res::ToolMod
754754
| Res::NonMacroAttr(..)
755755
| Res::SelfCtor(..)
@@ -814,7 +814,7 @@ impl<'tcx> SaveContext<'tcx> {
814814

815815
fn lookup_def_id(&self, ref_id: hir::HirId) -> Option<DefId> {
816816
match self.get_path_res(ref_id) {
817-
Res::PrimTy(_) | Res::SelfTy(..) | Res::Err => None,
817+
Res::PrimTy(_) | Res::SelfTy { .. } | Res::Err => None,
818818
def => def.opt_def_id(),
819819
}
820820
}

compiler/rustc_save_analysis/src/sig.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ impl<'hir> Sig for hir::Path<'hir> {
573573
let res = scx.get_path_res(id.ok_or("Missing id for Path")?);
574574

575575
let (name, start, end) = match res {
576-
Res::PrimTy(..) | Res::SelfTy(..) | Res::Err => {
576+
Res::PrimTy(..) | Res::SelfTy { .. } | Res::Err => {
577577
return Ok(Signature { text: path_to_string(self), defs: vec![], refs: vec![] });
578578
}
579579
Res::Def(DefKind::AssocConst | DefKind::Variant | DefKind::Ctor(..), _) => {

0 commit comments

Comments
 (0)