Skip to content

Commit 11ec2a4

Browse files
committed
extract Res to generics_of def_id conversion
1 parent a240ccd commit 11ec2a4

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

compiler/rustc_middle/src/ty/util.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_data_structures::intern::Interned;
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1818
use rustc_errors::ErrorReported;
1919
use rustc_hir as hir;
20-
use rustc_hir::def::DefKind;
20+
use rustc_hir::def::{CtorOf, DefKind, Res};
2121
use rustc_hir::def_id::DefId;
2222
use rustc_macros::HashStable;
2323
use rustc_query_system::ich::NodeIdHashingMode;
@@ -146,6 +146,37 @@ impl<'tcx> TyCtxt<'tcx> {
146146
hasher.finish()
147147
}
148148

149+
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
150+
match res {
151+
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
152+
Some(self.parent(def_id).and_then(|def_id| self.parent(def_id)).unwrap())
153+
}
154+
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
155+
Some(self.parent(def_id).unwrap())
156+
}
157+
// Other `DefKind`s don't have generics and would ICE when calling
158+
// `generics_of`.
159+
Res::Def(
160+
DefKind::Struct
161+
| DefKind::Union
162+
| DefKind::Enum
163+
| DefKind::Trait
164+
| DefKind::OpaqueTy
165+
| DefKind::TyAlias
166+
| DefKind::ForeignTy
167+
| DefKind::TraitAlias
168+
| DefKind::AssocTy
169+
| DefKind::Fn
170+
| DefKind::AssocFn
171+
| DefKind::AssocConst
172+
| DefKind::Impl,
173+
def_id,
174+
) => Some(def_id),
175+
Res::Err => None,
176+
_ => None,
177+
}
178+
}
179+
149180
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
150181
if let ty::Adt(def, substs) = *ty.kind() {
151182
for field in def.all_fields() {

compiler/rustc_typeck/src/collect/type_of.rs

+5-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use rustc_errors::{Applicability, ErrorReported, StashKey};
22
use rustc_hir as hir;
3-
use rustc_hir::def::CtorOf;
4-
use rustc_hir::def::{DefKind, Res};
3+
use rustc_hir::def::Res;
54
use rustc_hir::def_id::{DefId, LocalDefId};
65
use rustc_hir::intravisit;
76
use rustc_hir::intravisit::Visitor;
87
use rustc_hir::{HirId, Node};
98
use rustc_middle::hir::nested_filter;
109
use rustc_middle::ty::subst::InternalSubsts;
1110
use rustc_middle::ty::util::IntTypeExt;
12-
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable, TypeFolder};
11+
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeFolder};
1312
use rustc_span::symbol::Ident;
1413
use rustc_span::{Span, DUMMY_SP};
1514

@@ -198,38 +197,9 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
198197
// Try to use the segment resolution if it is valid, otherwise we
199198
// default to the path resolution.
200199
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
201-
let generics = match res {
202-
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx
203-
.generics_of(tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap()),
204-
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
205-
tcx.generics_of(tcx.parent(def_id).unwrap())
206-
}
207-
// Other `DefKind`s don't have generics and would ICE when calling
208-
// `generics_of`.
209-
Res::Def(
210-
DefKind::Struct
211-
| DefKind::Union
212-
| DefKind::Enum
213-
| DefKind::Trait
214-
| DefKind::OpaqueTy
215-
| DefKind::TyAlias
216-
| DefKind::ForeignTy
217-
| DefKind::TraitAlias
218-
| DefKind::AssocTy
219-
| DefKind::Fn
220-
| DefKind::AssocFn
221-
| DefKind::AssocConst
222-
| DefKind::Impl,
223-
def_id,
224-
) => tcx.generics_of(def_id),
225-
Res::Err => {
226-
tcx.sess.delay_span_bug(tcx.def_span(def_id), "anon const with Res::Err");
227-
return None;
228-
}
229-
_ => {
230-
// If the user tries to specify generics on a type that does not take them,
231-
// e.g. `usize<T>`, we may hit this branch, in which case we treat it as if
232-
// no arguments have been passed. An error should already have been emitted.
200+
let generics = match tcx.res_generics_def_id(res) {
201+
Some(def_id) => tcx.generics_of(def_id),
202+
None => {
233203
tcx.sess.delay_span_bug(
234204
tcx.def_span(def_id),
235205
&format!("unexpected anon const res {:?} in path: {:?}", res, path),

0 commit comments

Comments
 (0)