Skip to content

Commit

Permalink
Auto merge of #48939 - wesleywiser:incr_query_wf_checking, r=<try>
Browse files Browse the repository at this point in the history
Querify WF-checking so it can be cached

r? @michaelwoerister
  • Loading branch information
bors committed Mar 12, 2018
2 parents 222b0eb + 2b3d2a5 commit 843c304
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 33 deletions.
3 changes: 3 additions & 0 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@ define_dep_nodes!( <'tcx>
[] GetPanicStrategy(CrateNum),
[] IsNoBuiltins(CrateNum),
[] ImplDefaultness(DefId),
[] CheckItemWellFormed(DefId),
[] CheckTraitItemWellFormed(DefId),
[] CheckImplItemWellFormed(DefId),
[] ReachableNonGenerics(CrateNum),
[] NativeLibraries(CrateNum),
[] PluginRegistrarFn(CrateNum),
Expand Down
4 changes: 4 additions & 0 deletions src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ define_maps! { <'tcx>

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

[] fn check_item_well_formed: CheckItemWellFormed(DefId) -> (),
[] fn check_trait_item_well_formed: CheckTraitItemWellFormed(DefId) -> (),
[] fn check_impl_item_well_formed: CheckImplItemWellFormed(DefId) -> (),

// The DefIds of all non-generic functions and statics in the given crate
// that can be reached from outside the crate.
//
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); }
DepKind::CheckItemWellFormed => { force!(check_item_well_formed, def_id!()); }
DepKind::CheckTraitItemWellFormed => { force!(check_trait_item_well_formed, def_id!()); }
DepKind::CheckImplItemWellFormed => { force!(check_impl_item_well_formed, def_id!()); }
DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
Expand Down
15 changes: 15 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,13 +718,28 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
})?)
}

fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
wfcheck::CheckTypeWellFormed::new(tcx).check_item_well_formed(def_id);
}

fn check_trait_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
wfcheck::CheckTypeWellFormed::new(tcx).check_trait_item(def_id);
}

fn check_impl_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
wfcheck::CheckTypeWellFormed::new(tcx).check_impl_item(def_id);
}

pub fn provide(providers: &mut Providers) {
*providers = Providers {
typeck_item_bodies,
typeck_tables_of,
has_typeck_tables,
adt_destructor,
used_trait_imports,
check_item_well_formed,
check_trait_item_well_formed,
check_impl_item_well_formed,
..*providers
};
}
Expand Down
97 changes: 64 additions & 33 deletions src/librustc_typeck/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ use errors::{DiagnosticBuilder, DiagnosticId};
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use rustc::hir;

pub struct CheckTypeWellFormedVisitor<'a, 'tcx:'a> {
pub struct CheckTypeWellFormed<'a, 'tcx:'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
code: ObligationCauseCode<'tcx>,
}

/// Helper type of a temporary returned by .for_item(...).
/// Necessary because we can't write the following bound:
/// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(FnCtxt<'b, 'gcx, 'tcx>).
struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
inherited: super::InheritedBuilder<'a, 'gcx, 'tcx>,
code: ObligationCauseCode<'gcx>,
id: ast::NodeId,
span: Span,
param_env: ty::ParamEnv<'tcx>,
Expand All @@ -45,30 +43,27 @@ struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
fn with_fcx<F>(&'tcx mut self, f: F) where
F: for<'b> FnOnce(&FnCtxt<'b, 'gcx, 'tcx>,
&mut CheckTypeWellFormedVisitor<'b, 'gcx>) -> Vec<Ty<'tcx>>
&mut CheckTypeWellFormed<'b, 'gcx>) -> Vec<Ty<'tcx>>
{
let code = self.code.clone();
let id = self.id;
let span = self.span;
let param_env = self.param_env;
self.inherited.enter(|inh| {
let fcx = FnCtxt::new(&inh, param_env, id);
let wf_tys = f(&fcx, &mut CheckTypeWellFormedVisitor {
let wf_tys = f(&fcx, &mut CheckTypeWellFormed {
tcx: fcx.tcx.global_tcx(),
code,
});
fcx.select_all_obligations_or_error();
fcx.regionck_item(id, span, &wf_tys);
});
}
}

impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
impl<'a, 'gcx> CheckTypeWellFormed<'a, 'gcx> {
pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
-> CheckTypeWellFormedVisitor<'a, 'gcx> {
CheckTypeWellFormedVisitor {
-> CheckTypeWellFormed<'a, 'gcx> {
CheckTypeWellFormed {
tcx,
code: ObligationCauseCode::MiscObligation
}
}

Expand All @@ -83,11 +78,14 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
/// We do this check as a pre-pass before checking fn bodies because if these constraints are
/// not included it frequently leads to confusing errors in fn bodies. So it's better to check
/// the types first.
fn check_item_well_formed(&mut self, item: &hir::Item) {
pub fn check_item_well_formed(&mut self, def_id: DefId) {
let tcx = self.tcx;
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
let item = tcx.hir.expect_item(node_id);

debug!("check_item_well_formed(it.id={}, it.name={})",
item.id,
tcx.item_path_str(tcx.hir.local_def_id(item.id)));
tcx.item_path_str(def_id));

match item.node {
// Right now we check that every default trait implementation
Expand Down Expand Up @@ -161,11 +159,34 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
}
}

pub fn check_trait_item(&mut self, def_id: DefId) {
let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
let trait_item = self.tcx.hir.expect_trait_item(node_id);

let method_sig = match trait_item.node {
hir::TraitItemKind::Method(ref sig, _) => Some(sig),
_ => None
};
CheckTypeWellFormed::new(self.tcx)
.check_associated_item(trait_item.id, trait_item.span, method_sig);
}

pub fn check_impl_item(&mut self, def_id: DefId) {
let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap();
let impl_item = self.tcx.hir.expect_impl_item(node_id);

let method_sig = match impl_item.node {
hir::ImplItemKind::Method(ref sig, _) => Some(sig),
_ => None
};
self.check_associated_item(impl_item.id, impl_item.span, method_sig);
}

fn check_associated_item(&mut self,
item_id: ast::NodeId,
span: Span,
sig_if_method: Option<&hir::MethodSig>) {
let code = self.code.clone();
let code = ObligationCauseCode::MiscObligation;
self.for_id(item_id, span).with_fcx(|fcx, this| {
let item = fcx.tcx.associated_item(fcx.tcx.hir.local_def_id(item_id));

Expand Down Expand Up @@ -213,7 +234,6 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
let def_id = self.tcx.hir.local_def_id(id);
CheckWfFcxBuilder {
inherited: Inherited::build(self.tcx, def_id),
code: self.code.clone(),
id,
span,
param_env: self.tcx.param_env(def_id),
Expand Down Expand Up @@ -265,7 +285,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {

// All field types must be well-formed.
for field in &variant.fields {
fcx.register_wf_obligation(field.ty, field.span, this.code.clone())
fcx.register_wf_obligation(field.ty, field.span,
ObligationCauseCode::MiscObligation)
}
}

Expand Down Expand Up @@ -300,11 +321,11 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
{
debug!("check_item_type: {:?}", item);

self.for_item(item).with_fcx(|fcx, this| {
self.for_item(item).with_fcx(|fcx, _this| {
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item.id));
let item_ty = fcx.normalize_associated_types_in(item.span, &ty);

fcx.register_wf_obligation(item_ty, item.span, this.code.clone());
fcx.register_wf_obligation(item_ty, item.span, ObligationCauseCode::MiscObligation);

vec![] // no implied bounds in a const etc
});
Expand Down Expand Up @@ -339,7 +360,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
None => {
let self_ty = fcx.tcx.type_of(item_def_id);
let self_ty = fcx.normalize_associated_types_in(item.span, &self_ty);
fcx.register_wf_obligation(self_ty, ast_self_ty.span, this.code.clone());
fcx.register_wf_obligation(self_ty, ast_self_ty.span,
ObligationCauseCode::MiscObligation);
}
}

Expand Down Expand Up @@ -374,7 +396,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
// parameter includes another (e.g., <T, U = T>). In those cases, we can't
// be sure if it will error or not as user might always specify the other.
if !ty.needs_subst() {
fcx.register_wf_obligation(ty, fcx.tcx.def_span(d), self.code.clone());
fcx.register_wf_obligation(ty, fcx.tcx.def_span(d),
ObligationCauseCode::MiscObligation);
}
}

Expand Down Expand Up @@ -458,11 +481,11 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
let sig = fcx.tcx.liberate_late_bound_regions(def_id, &sig);

for input_ty in sig.inputs() {
fcx.register_wf_obligation(&input_ty, span, self.code.clone());
fcx.register_wf_obligation(&input_ty, span, ObligationCauseCode::MiscObligation);
}
implied_bounds.extend(sig.inputs());

fcx.register_wf_obligation(sig.output(), span, self.code.clone());
fcx.register_wf_obligation(sig.output(), span, ObligationCauseCode::MiscObligation);

// FIXME(#25759) return types should not be implied bounds
implied_bounds.push(sig.output());
Expand Down Expand Up @@ -648,34 +671,42 @@ fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
}
}

pub struct CheckTypeWellFormedVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
-> CheckTypeWellFormedVisitor<'a, 'gcx> {
CheckTypeWellFormedVisitor {
tcx,
}
}
}

impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
NestedVisitorMap::None
}

fn visit_item(&mut self, i: &hir::Item) {
debug!("visit_item: {:?}", i);
self.check_item_well_formed(i);
let def_id = self.tcx.hir.local_def_id(i.id);
ty::maps::queries::check_item_well_formed::ensure(self.tcx, def_id);
intravisit::walk_item(self, i);
}

fn visit_trait_item(&mut self, trait_item: &'v hir::TraitItem) {
debug!("visit_trait_item: {:?}", trait_item);
let method_sig = match trait_item.node {
hir::TraitItemKind::Method(ref sig, _) => Some(sig),
_ => None
};
self.check_associated_item(trait_item.id, trait_item.span, method_sig);
let def_id = self.tcx.hir.local_def_id(trait_item.id);
ty::maps::queries::check_trait_item_well_formed::ensure(self.tcx, def_id);
intravisit::walk_trait_item(self, trait_item)
}

fn visit_impl_item(&mut self, impl_item: &'v hir::ImplItem) {
debug!("visit_impl_item: {:?}", impl_item);
let method_sig = match impl_item.node {
hir::ImplItemKind::Method(ref sig, _) => Some(sig),
_ => None
};
self.check_associated_item(impl_item.id, impl_item.span, method_sig);
let def_id = self.tcx.hir.local_def_id(impl_item.id);
ty::maps::queries::check_impl_item_well_formed::ensure(self.tcx, def_id);
intravisit::walk_impl_item(self, impl_item)
}
}
Expand Down

0 comments on commit 843c304

Please sign in to comment.