Skip to content

Commit 4b2d9e6

Browse files
committed
Remove TypeckTables::empty(None) and make hir_owner non-optional.
1 parent 547be8c commit 4b2d9e6

File tree

7 files changed

+98
-121
lines changed

7 files changed

+98
-121
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16841684
// Attempt to obtain the span of the parameter so we can
16851685
// suggest adding an explicit lifetime bound to it.
16861686
let generics =
1687-
self.in_progress_tables.and_then(|table| table.borrow().hir_owner).map(|table_owner| {
1687+
self.in_progress_tables.map(|table| table.borrow().hir_owner).map(|table_owner| {
16881688
let hir_id = hir.as_local_hir_id(table_owner);
16891689
let parent_id = hir.get_parent_item(hir_id);
16901690
(

src/librustc_infer/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
588588
/// Used only by `rustc_typeck` during body type-checking/inference,
589589
/// will initialize `in_progress_tables` with fresh `TypeckTables`.
590590
pub fn with_fresh_in_progress_tables(mut self, table_owner: LocalDefId) -> Self {
591-
self.fresh_tables = Some(RefCell::new(ty::TypeckTables::empty(Some(table_owner))));
591+
self.fresh_tables = Some(RefCell::new(ty::TypeckTables::new(table_owner)));
592592
self
593593
}
594594

src/librustc_middle/ty/context.rs

+27-42
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub struct CommonConsts<'tcx> {
188188
}
189189

190190
pub struct LocalTableInContext<'a, V> {
191-
hir_owner: Option<LocalDefId>,
191+
hir_owner: LocalDefId,
192192
data: &'a ItemLocalMap<V>,
193193
}
194194

@@ -199,42 +199,27 @@ pub struct LocalTableInContext<'a, V> {
199199
/// would be in a different frame of reference and using its `local_id`
200200
/// would result in lookup errors, or worse, in silently wrong data being
201201
/// stored/returned.
202-
fn validate_hir_id_for_typeck_tables(
203-
hir_owner: Option<LocalDefId>,
204-
hir_id: hir::HirId,
205-
mut_access: bool,
206-
) {
207-
if let Some(hir_owner) = hir_owner {
208-
if hir_id.owner != hir_owner {
209-
ty::tls::with(|tcx| {
210-
bug!(
211-
"node {} with HirId::owner {:?} cannot be placed in TypeckTables with hir_owner {:?}",
212-
tcx.hir().node_to_string(hir_id),
213-
hir_id.owner,
214-
hir_owner
215-
)
216-
});
217-
}
218-
} else {
219-
// We use "Null Object" TypeckTables in some of the analysis passes.
220-
// These are just expected to be empty and their `hir_owner` is
221-
// `None`. Therefore we cannot verify whether a given `HirId` would
222-
// be a valid key for the given table. Instead we make sure that
223-
// nobody tries to write to such a Null Object table.
224-
if mut_access {
225-
bug!("access to invalid TypeckTables")
226-
}
202+
fn validate_hir_id_for_typeck_tables(hir_owner: LocalDefId, hir_id: hir::HirId) {
203+
if hir_id.owner != hir_owner {
204+
ty::tls::with(|tcx| {
205+
bug!(
206+
"node {} with HirId::owner {:?} cannot be placed in TypeckTables with hir_owner {:?}",
207+
tcx.hir().node_to_string(hir_id),
208+
hir_id.owner,
209+
hir_owner
210+
)
211+
});
227212
}
228213
}
229214

230215
impl<'a, V> LocalTableInContext<'a, V> {
231216
pub fn contains_key(&self, id: hir::HirId) -> bool {
232-
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
217+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
233218
self.data.contains_key(&id.local_id)
234219
}
235220

236221
pub fn get(&self, id: hir::HirId) -> Option<&V> {
237-
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
222+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
238223
self.data.get(&id.local_id)
239224
}
240225

@@ -252,28 +237,28 @@ impl<'a, V> ::std::ops::Index<hir::HirId> for LocalTableInContext<'a, V> {
252237
}
253238

254239
pub struct LocalTableInContextMut<'a, V> {
255-
hir_owner: Option<LocalDefId>,
240+
hir_owner: LocalDefId,
256241
data: &'a mut ItemLocalMap<V>,
257242
}
258243

259244
impl<'a, V> LocalTableInContextMut<'a, V> {
260245
pub fn get_mut(&mut self, id: hir::HirId) -> Option<&mut V> {
261-
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
246+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
262247
self.data.get_mut(&id.local_id)
263248
}
264249

265250
pub fn entry(&mut self, id: hir::HirId) -> Entry<'_, hir::ItemLocalId, V> {
266-
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
251+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
267252
self.data.entry(id.local_id)
268253
}
269254

270255
pub fn insert(&mut self, id: hir::HirId, val: V) -> Option<V> {
271-
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
256+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
272257
self.data.insert(id.local_id, val)
273258
}
274259

275260
pub fn remove(&mut self, id: hir::HirId) -> Option<V> {
276-
validate_hir_id_for_typeck_tables(self.hir_owner, id, true);
261+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
277262
self.data.remove(&id.local_id)
278263
}
279264
}
@@ -324,7 +309,7 @@ pub struct GeneratorInteriorTypeCause<'tcx> {
324309
#[derive(RustcEncodable, RustcDecodable, Debug)]
325310
pub struct TypeckTables<'tcx> {
326311
/// The `HirId::owner` all `ItemLocalId`s in this table are relative to.
327-
pub hir_owner: Option<LocalDefId>,
312+
pub hir_owner: LocalDefId,
328313

329314
/// Resolved definitions for `<T>::X` associated paths and
330315
/// method calls, including those of overloaded operators.
@@ -432,7 +417,7 @@ pub struct TypeckTables<'tcx> {
432417
}
433418

434419
impl<'tcx> TypeckTables<'tcx> {
435-
pub fn empty(hir_owner: Option<LocalDefId>) -> TypeckTables<'tcx> {
420+
pub fn new(hir_owner: LocalDefId) -> TypeckTables<'tcx> {
436421
TypeckTables {
437422
hir_owner,
438423
type_dependent_defs: Default::default(),
@@ -474,7 +459,7 @@ impl<'tcx> TypeckTables<'tcx> {
474459
}
475460

476461
pub fn type_dependent_def(&self, id: HirId) -> Option<(DefKind, DefId)> {
477-
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
462+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
478463
self.type_dependent_defs.get(&id.local_id).cloned().and_then(|r| r.ok())
479464
}
480465

@@ -521,7 +506,7 @@ impl<'tcx> TypeckTables<'tcx> {
521506
}
522507

523508
pub fn node_type_opt(&self, id: hir::HirId) -> Option<Ty<'tcx>> {
524-
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
509+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
525510
self.node_types.get(&id.local_id).cloned()
526511
}
527512

@@ -530,12 +515,12 @@ impl<'tcx> TypeckTables<'tcx> {
530515
}
531516

532517
pub fn node_substs(&self, id: hir::HirId) -> SubstsRef<'tcx> {
533-
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
518+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
534519
self.node_substs.get(&id.local_id).cloned().unwrap_or_else(|| InternalSubsts::empty())
535520
}
536521

537522
pub fn node_substs_opt(&self, id: hir::HirId) -> Option<SubstsRef<'tcx>> {
538-
validate_hir_id_for_typeck_tables(self.hir_owner, id, false);
523+
validate_hir_id_for_typeck_tables(self.hir_owner, id);
539524
self.node_substs.get(&id.local_id).cloned()
540525
}
541526

@@ -578,7 +563,7 @@ impl<'tcx> TypeckTables<'tcx> {
578563
}
579564

580565
pub fn expr_adjustments(&self, expr: &hir::Expr<'_>) -> &[ty::adjustment::Adjustment<'tcx>] {
581-
validate_hir_id_for_typeck_tables(self.hir_owner, expr.hir_id, false);
566+
validate_hir_id_for_typeck_tables(self.hir_owner, expr.hir_id);
582567
self.adjustments.get(&expr.hir_id.local_id).map_or(&[], |a| &a[..])
583568
}
584569

@@ -657,7 +642,7 @@ impl<'tcx> TypeckTables<'tcx> {
657642
}
658643

659644
pub fn is_coercion_cast(&self, hir_id: hir::HirId) -> bool {
660-
validate_hir_id_for_typeck_tables(self.hir_owner, hir_id, true);
645+
validate_hir_id_for_typeck_tables(self.hir_owner, hir_id);
661646
self.coercion_casts.contains(&hir_id.local_id)
662647
}
663648

@@ -710,7 +695,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
710695
hash_stable_hashmap(hcx, hasher, upvar_capture_map, |up_var_id, hcx| {
711696
let ty::UpvarId { var_path, closure_expr_id } = *up_var_id;
712697

713-
assert_eq!(Some(var_path.hir_id.owner), hir_owner);
698+
assert_eq!(var_path.hir_id.owner, hir_owner);
714699

715700
(
716701
hcx.local_def_path_hash(var_path.hir_id.owner),

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
14071407
);
14081408
let query_tables;
14091409
let tables: &TypeckTables<'tcx> = match &in_progress_tables {
1410-
Some(t) if t.hir_owner.map(|owner| owner.to_def_id()) == Some(generator_did_root) => t,
1410+
Some(t) if t.hir_owner.to_def_id() == generator_did_root => t,
14111411
_ => {
14121412
query_tables = self.tcx.typeck_tables_of(generator_did.expect_local());
14131413
&query_tables

src/librustc_typeck/check/method/suggest.rs

+62-65
Original file line numberDiff line numberDiff line change
@@ -1039,22 +1039,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10391039
let mut suggested = false;
10401040
if let (Some(ref param), Some(ref table)) = (param_type, self.in_progress_tables) {
10411041
let table_owner = table.borrow().hir_owner;
1042-
if let Some(table_owner) = table_owner {
1043-
let generics = self.tcx.generics_of(table_owner.to_def_id());
1044-
let type_param = generics.type_param(param, self.tcx);
1045-
let hir = &self.tcx.hir();
1046-
if let Some(def_id) = type_param.def_id.as_local() {
1047-
let id = hir.as_local_hir_id(def_id);
1048-
// Get the `hir::Param` to verify whether it already has any bounds.
1049-
// We do this to avoid suggesting code that ends up as `T: FooBar`,
1050-
// instead we suggest `T: Foo + Bar` in that case.
1051-
match hir.get(id) {
1052-
Node::GenericParam(ref param) => {
1053-
let mut impl_trait = false;
1054-
let has_bounds = if let hir::GenericParamKind::Type {
1055-
synthetic: Some(_),
1056-
..
1057-
} = &param.kind
1042+
let generics = self.tcx.generics_of(table_owner.to_def_id());
1043+
let type_param = generics.type_param(param, self.tcx);
1044+
let hir = &self.tcx.hir();
1045+
if let Some(def_id) = type_param.def_id.as_local() {
1046+
let id = hir.as_local_hir_id(def_id);
1047+
// Get the `hir::Param` to verify whether it already has any bounds.
1048+
// We do this to avoid suggesting code that ends up as `T: FooBar`,
1049+
// instead we suggest `T: Foo + Bar` in that case.
1050+
match hir.get(id) {
1051+
Node::GenericParam(ref param) => {
1052+
let mut impl_trait = false;
1053+
let has_bounds =
1054+
if let hir::GenericParamKind::Type { synthetic: Some(_), .. } =
1055+
&param.kind
10581056
{
10591057
// We've found `fn foo(x: impl Trait)` instead of
10601058
// `fn foo<T>(x: T)`. We want to suggest the correct
@@ -1065,64 +1063,63 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10651063
} else {
10661064
param.bounds.get(0)
10671065
};
1068-
let sp = hir.span(id);
1069-
let sp = if let Some(first_bound) = has_bounds {
1070-
// `sp` only covers `T`, change it so that it covers
1071-
// `T:` when appropriate
1072-
sp.until(first_bound.span())
1073-
} else {
1074-
sp
1075-
};
1076-
let trait_def_ids: FxHashSet<DefId> = param
1077-
.bounds
1078-
.iter()
1079-
.filter_map(|bound| Some(bound.trait_ref()?.trait_def_id()?))
1080-
.collect();
1081-
if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
1082-
err.span_suggestions(
1083-
sp,
1084-
&message(format!(
1085-
"restrict type parameter `{}` with",
1086-
param.name.ident(),
1087-
)),
1088-
candidates.iter().map(|t| {
1089-
format!(
1090-
"{}{} {}{}",
1091-
param.name.ident(),
1092-
if impl_trait { " +" } else { ":" },
1093-
self.tcx.def_path_str(t.def_id),
1094-
if has_bounds.is_some() { " + " } else { "" },
1095-
)
1096-
}),
1097-
Applicability::MaybeIncorrect,
1098-
);
1099-
}
1100-
suggested = true;
1101-
}
1102-
Node::Item(hir::Item {
1103-
kind: hir::ItemKind::Trait(.., bounds, _),
1104-
ident,
1105-
..
1106-
}) => {
1107-
let (sp, sep, article) = if bounds.is_empty() {
1108-
(ident.span.shrink_to_hi(), ":", "a")
1109-
} else {
1110-
(bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
1111-
};
1066+
let sp = hir.span(id);
1067+
let sp = if let Some(first_bound) = has_bounds {
1068+
// `sp` only covers `T`, change it so that it covers
1069+
// `T:` when appropriate
1070+
sp.until(first_bound.span())
1071+
} else {
1072+
sp
1073+
};
1074+
let trait_def_ids: FxHashSet<DefId> = param
1075+
.bounds
1076+
.iter()
1077+
.filter_map(|bound| Some(bound.trait_ref()?.trait_def_id()?))
1078+
.collect();
1079+
if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
11121080
err.span_suggestions(
11131081
sp,
1114-
&message(format!("add {} supertrait for", article)),
1082+
&message(format!(
1083+
"restrict type parameter `{}` with",
1084+
param.name.ident(),
1085+
)),
11151086
candidates.iter().map(|t| {
1116-
format!("{} {}", sep, self.tcx.def_path_str(t.def_id),)
1087+
format!(
1088+
"{}{} {}{}",
1089+
param.name.ident(),
1090+
if impl_trait { " +" } else { ":" },
1091+
self.tcx.def_path_str(t.def_id),
1092+
if has_bounds.is_some() { " + " } else { "" },
1093+
)
11171094
}),
11181095
Applicability::MaybeIncorrect,
11191096
);
1120-
suggested = true;
11211097
}
1122-
_ => {}
1098+
suggested = true;
1099+
}
1100+
Node::Item(hir::Item {
1101+
kind: hir::ItemKind::Trait(.., bounds, _),
1102+
ident,
1103+
..
1104+
}) => {
1105+
let (sp, sep, article) = if bounds.is_empty() {
1106+
(ident.span.shrink_to_hi(), ":", "a")
1107+
} else {
1108+
(bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
1109+
};
1110+
err.span_suggestions(
1111+
sp,
1112+
&message(format!("add {} supertrait for", article)),
1113+
candidates.iter().map(|t| {
1114+
format!("{} {}", sep, self.tcx.def_path_str(t.def_id),)
1115+
}),
1116+
Applicability::MaybeIncorrect,
1117+
);
1118+
suggested = true;
11231119
}
1120+
_ => {}
11241121
}
1125-
};
1122+
}
11261123
}
11271124

11281125
if !suggested {

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ fn typeck_tables_of_with_fallback<'tcx>(
11261126

11271127
// Consistency check our TypeckTables instance can hold all ItemLocalIds
11281128
// it will need to hold.
1129-
assert_eq!(tables.hir_owner, Some(id.owner));
1129+
assert_eq!(tables.hir_owner, id.owner);
11301130

11311131
tables
11321132
}

0 commit comments

Comments
 (0)