Skip to content

Commit 86a123c

Browse files
committed
Queryify check_item_well_formed
Fixes #46753
1 parent 450d35f commit 86a123c

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ define_dep_nodes!( <'tcx>
579579
[] GetPanicStrategy(CrateNum),
580580
[] IsNoBuiltins(CrateNum),
581581
[] ImplDefaultness(DefId),
582+
[] CheckItemWellFormed(DefId),
582583
[] ReachableNonGenerics(CrateNum),
583584
[] NativeLibraries(CrateNum),
584585
[] PluginRegistrarFn(CrateNum),

src/librustc/ty/maps/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ define_maps! { <'tcx>
298298

299299
[] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness,
300300

301+
[] fn check_item_well_formed: CheckItemWellFormed(DefId) -> (),
302+
301303
// The DefIds of all non-generic functions and statics in the given crate
302304
// that can be reached from outside the crate.
303305
//

src/librustc/ty/maps/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
871871
DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
872872
DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
873873
DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); }
874+
DepKind::CheckItemWellFormed => { force!(check_item_well_formed, def_id!()); }
874875
DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
875876
DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
876877
DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }

src/librustc_typeck/check/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,18 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
718718
})?)
719719
}
720720

721+
fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
722+
wfcheck::CheckTypeWellFormed::new(tcx).check_item_well_formed(def_id);
723+
}
724+
721725
pub fn provide(providers: &mut Providers) {
722726
*providers = Providers {
723727
typeck_item_bodies,
724728
typeck_tables_of,
725729
has_typeck_tables,
726730
adt_destructor,
727731
used_trait_imports,
732+
check_item_well_formed,
728733
..*providers
729734
};
730735
}

src/librustc_typeck/check/wfcheck.rs

+36-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use errors::{DiagnosticBuilder, DiagnosticId};
2626
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
2727
use rustc::hir;
2828

29-
pub struct CheckTypeWellFormedVisitor<'a, 'tcx:'a> {
29+
pub struct CheckTypeWellFormed<'a, 'tcx:'a> {
3030
tcx: TyCtxt<'a, 'tcx, 'tcx>,
3131
}
3232

@@ -43,14 +43,14 @@ struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
4343
impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
4444
fn with_fcx<F>(&'tcx mut self, f: F) where
4545
F: for<'b> FnOnce(&FnCtxt<'b, 'gcx, 'tcx>,
46-
&mut CheckTypeWellFormedVisitor<'b, 'gcx>) -> Vec<Ty<'tcx>>
46+
&mut CheckTypeWellFormed<'b, 'gcx>) -> Vec<Ty<'tcx>>
4747
{
4848
let id = self.id;
4949
let span = self.span;
5050
let param_env = self.param_env;
5151
self.inherited.enter(|inh| {
5252
let fcx = FnCtxt::new(&inh, param_env, id);
53-
let wf_tys = f(&fcx, &mut CheckTypeWellFormedVisitor {
53+
let wf_tys = f(&fcx, &mut CheckTypeWellFormed {
5454
tcx: fcx.tcx.global_tcx(),
5555
});
5656
fcx.select_all_obligations_or_error();
@@ -59,10 +59,10 @@ impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
5959
}
6060
}
6161

62-
impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
62+
impl<'a, 'gcx> CheckTypeWellFormed<'a, 'gcx> {
6363
pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
64-
-> CheckTypeWellFormedVisitor<'a, 'gcx> {
65-
CheckTypeWellFormedVisitor {
64+
-> CheckTypeWellFormed<'a, 'gcx> {
65+
CheckTypeWellFormed {
6666
tcx,
6767
}
6868
}
@@ -78,11 +78,14 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
7878
/// We do this check as a pre-pass before checking fn bodies because if these constraints are
7979
/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
8080
/// the types first.
81-
fn check_item_well_formed(&mut self, item: &hir::Item) {
81+
pub fn check_item_well_formed(&mut self, def_id: DefId) {
8282
let tcx = self.tcx;
83+
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
84+
let item = tcx.hir.expect_item(node_id);
85+
8386
debug!("check_item_well_formed(it.id={}, it.name={})",
8487
item.id,
85-
tcx.item_path_str(tcx.hir.local_def_id(item.id)));
88+
tcx.item_path_str(def_id));
8689

8790
match item.node {
8891
// Right now we check that every default trait implementation
@@ -259,7 +262,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
259262

260263
// All field types must be well-formed.
261264
for field in &variant.fields {
262-
fcx.register_wf_obligation(field.ty, field.span, ObligationCauseCode::MiscObligation)
265+
fcx.register_wf_obligation(field.ty, field.span,
266+
ObligationCauseCode::MiscObligation)
263267
}
264268
}
265269

@@ -333,7 +337,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
333337
None => {
334338
let self_ty = fcx.tcx.type_of(item_def_id);
335339
let self_ty = fcx.normalize_associated_types_in(item.span, &self_ty);
336-
fcx.register_wf_obligation(self_ty, ast_self_ty.span, ObligationCauseCode::MiscObligation);
340+
fcx.register_wf_obligation(self_ty, ast_self_ty.span,
341+
ObligationCauseCode::MiscObligation);
337342
}
338343
}
339344

@@ -368,7 +373,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
368373
// parameter includes another (e.g., <T, U = T>). In those cases, we can't
369374
// be sure if it will error or not as user might always specify the other.
370375
if !ty.needs_subst() {
371-
fcx.register_wf_obligation(ty, fcx.tcx.def_span(d), ObligationCauseCode::MiscObligation);
376+
fcx.register_wf_obligation(ty, fcx.tcx.def_span(d),
377+
ObligationCauseCode::MiscObligation);
372378
}
373379
}
374380

@@ -642,14 +648,28 @@ fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
642648
}
643649
}
644650

651+
pub struct CheckTypeWellFormedVisitor<'a, 'tcx: 'a> {
652+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
653+
}
654+
655+
impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
656+
pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
657+
-> CheckTypeWellFormedVisitor<'a, 'gcx> {
658+
CheckTypeWellFormedVisitor {
659+
tcx,
660+
}
661+
}
662+
}
663+
645664
impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
646665
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
647666
NestedVisitorMap::None
648667
}
649668

650669
fn visit_item(&mut self, i: &hir::Item) {
651670
debug!("visit_item: {:?}", i);
652-
self.check_item_well_formed(i);
671+
let def_id = self.tcx.hir.local_def_id(i.id);
672+
ty::maps::queries::check_item_well_formed::ensure(self.tcx, def_id);
653673
intravisit::walk_item(self, i);
654674
}
655675

@@ -659,7 +679,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
659679
hir::TraitItemKind::Method(ref sig, _) => Some(sig),
660680
_ => None
661681
};
662-
self.check_associated_item(trait_item.id, trait_item.span, method_sig);
682+
CheckTypeWellFormed::new(self.tcx)
683+
.check_associated_item(trait_item.id, trait_item.span, method_sig);
663684
intravisit::walk_trait_item(self, trait_item)
664685
}
665686

@@ -669,7 +690,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
669690
hir::ImplItemKind::Method(ref sig, _) => Some(sig),
670691
_ => None
671692
};
672-
self.check_associated_item(impl_item.id, impl_item.span, method_sig);
693+
CheckTypeWellFormed::new(self.tcx)
694+
.check_associated_item(impl_item.id, impl_item.span, method_sig);
673695
intravisit::walk_impl_item(self, impl_item)
674696
}
675697
}

0 commit comments

Comments
 (0)