Rollup merge of #48939 - wesleywiser:incr_query_wf_checking, r=michaelwoerister

Querify WF-checking so it can be cached

r? @michaelwoerister
This commit is contained in:
kennytm
2018-03-22 17:51:19 +08:00
5 changed files with 593 additions and 554 deletions

View File

@@ -579,6 +579,9 @@ define_dep_nodes!( <'tcx>
[] GetPanicStrategy(CrateNum), [] GetPanicStrategy(CrateNum),
[] IsNoBuiltins(CrateNum), [] IsNoBuiltins(CrateNum),
[] ImplDefaultness(DefId), [] ImplDefaultness(DefId),
[] CheckItemWellFormed(DefId),
[] CheckTraitItemWellFormed(DefId),
[] CheckImplItemWellFormed(DefId),
[] ReachableNonGenerics(CrateNum), [] ReachableNonGenerics(CrateNum),
[] NativeLibraries(CrateNum), [] NativeLibraries(CrateNum),
[] PluginRegistrarFn(CrateNum), [] PluginRegistrarFn(CrateNum),

View File

@@ -299,6 +299,10 @@ define_maps! { <'tcx>
[] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness, [] 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 // The DefIds of all non-generic functions and statics in the given crate
// that can be reached from outside the crate. // that can be reached from outside the crate.
// //

View File

@@ -871,6 +871,9 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); } DepKind::GetPanicStrategy => { force!(panic_strategy, krate!()); }
DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); } DepKind::IsNoBuiltins => { force!(is_no_builtins, krate!()); }
DepKind::ImplDefaultness => { force!(impl_defaultness, def_id!()); } 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::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
DepKind::NativeLibraries => { force!(native_libraries, krate!()); } DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); } DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }

View File

@@ -718,6 +718,18 @@ 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::check_item_well_formed(tcx, def_id);
}
fn check_trait_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
wfcheck::check_trait_item(tcx, def_id);
}
fn check_impl_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
wfcheck::check_impl_item(tcx, def_id);
}
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {
*providers = Providers { *providers = Providers {
typeck_item_bodies, typeck_item_bodies,
@@ -725,6 +737,9 @@ pub fn provide(providers: &mut Providers) {
has_typeck_tables, has_typeck_tables,
adt_destructor, adt_destructor,
used_trait_imports, used_trait_imports,
check_item_well_formed,
check_trait_item_well_formed,
check_impl_item_well_formed,
..*providers ..*providers
}; };
} }

View File

@@ -26,17 +26,11 @@ use errors::{DiagnosticBuilder, DiagnosticId};
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap}; use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use rustc::hir; use rustc::hir;
pub struct CheckTypeWellFormedVisitor<'a, 'tcx:'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
code: ObligationCauseCode<'tcx>,
}
/// Helper type of a temporary returned by .for_item(...). /// Helper type of a temporary returned by .for_item(...).
/// Necessary because we can't write the following bound: /// Necessary because we can't write the following bound:
/// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(FnCtxt<'b, 'gcx, 'tcx>). /// F: for<'b, 'tcx> where 'gcx: 'tcx FnOnce(FnCtxt<'b, 'gcx, 'tcx>).
struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
inherited: super::InheritedBuilder<'a, 'gcx, 'tcx>, inherited: super::InheritedBuilder<'a, 'gcx, 'tcx>,
code: ObligationCauseCode<'gcx>,
id: ast::NodeId, id: ast::NodeId,
span: Span, span: Span,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
@@ -45,33 +39,20 @@ struct CheckWfFcxBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> { impl<'a, 'gcx, 'tcx> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
fn with_fcx<F>(&'tcx mut self, f: F) where fn with_fcx<F>(&'tcx mut self, f: F) where
F: for<'b> FnOnce(&FnCtxt<'b, 'gcx, 'tcx>, F: for<'b> FnOnce(&FnCtxt<'b, 'gcx, 'tcx>,
&mut CheckTypeWellFormedVisitor<'b, 'gcx>) -> Vec<Ty<'tcx>> TyCtxt<'b, 'gcx, 'gcx>) -> Vec<Ty<'tcx>>
{ {
let code = self.code.clone();
let id = self.id; let id = self.id;
let span = self.span; let span = self.span;
let param_env = self.param_env; let param_env = self.param_env;
self.inherited.enter(|inh| { self.inherited.enter(|inh| {
let fcx = FnCtxt::new(&inh, param_env, id); let fcx = FnCtxt::new(&inh, param_env, id);
let wf_tys = f(&fcx, &mut CheckTypeWellFormedVisitor { let wf_tys = f(&fcx, fcx.tcx.global_tcx());
tcx: fcx.tcx.global_tcx(),
code,
});
fcx.select_all_obligations_or_error(); fcx.select_all_obligations_or_error();
fcx.regionck_item(id, span, &wf_tys); fcx.regionck_item(id, span, &wf_tys);
}); });
} }
} }
impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
pub fn new(tcx: TyCtxt<'a, 'gcx, 'gcx>)
-> CheckTypeWellFormedVisitor<'a, 'gcx> {
CheckTypeWellFormedVisitor {
tcx,
code: ObligationCauseCode::MiscObligation
}
}
/// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are /// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
/// well-formed, meaning that they do not require any constraints not declared in the struct /// well-formed, meaning that they do not require any constraints not declared in the struct
/// definition itself. For example, this definition would be illegal: /// definition itself. For example, this definition would be illegal:
@@ -83,11 +64,13 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
/// We do this check as a pre-pass before checking fn bodies because if these constraints are /// 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 /// not included it frequently leads to confusing errors in fn bodies. So it's better to check
/// the types first. /// the types first.
fn check_item_well_formed(&mut self, item: &hir::Item) { pub fn check_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, 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={})", debug!("check_item_well_formed(it.id={}, it.name={})",
item.id, item.id,
tcx.item_path_str(tcx.hir.local_def_id(item.id))); tcx.item_path_str(def_id));
match item.node { match item.node {
// Right now we check that every default trait implementation // Right now we check that every default trait implementation
@@ -114,7 +97,7 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
tcx.sess.span_err(item.span, "impls of auto traits cannot be default"); tcx.sess.span_err(item.span, "impls of auto traits cannot be default");
} }
if polarity == hir::ImplPolarity::Positive { if polarity == hir::ImplPolarity::Positive {
self.check_impl(item, self_ty, trait_ref); check_impl(tcx, item, self_ty, trait_ref);
} else { } else {
// FIXME(#27579) what amount of WF checking do we need for neg impls? // FIXME(#27579) what amount of WF checking do we need for neg impls?
if trait_ref.is_some() && !is_auto { if trait_ref.is_some() && !is_auto {
@@ -125,48 +108,70 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
} }
} }
hir::ItemFn(..) => { hir::ItemFn(..) => {
self.check_item_fn(item); check_item_fn(tcx, item);
} }
hir::ItemStatic(..) => { hir::ItemStatic(..) => {
self.check_item_type(item); check_item_type(tcx, item);
} }
hir::ItemConst(..) => { hir::ItemConst(..) => {
self.check_item_type(item); check_item_type(tcx, item);
} }
hir::ItemStruct(ref struct_def, ref ast_generics) => { hir::ItemStruct(ref struct_def, ref ast_generics) => {
self.check_type_defn(item, false, |fcx| { check_type_defn(tcx, item, false, |fcx| {
vec![fcx.non_enum_variant(struct_def)] vec![fcx.non_enum_variant(struct_def)]
}); });
self.check_variances_for_type_defn(item, ast_generics); check_variances_for_type_defn(tcx, item, ast_generics);
} }
hir::ItemUnion(ref struct_def, ref ast_generics) => { hir::ItemUnion(ref struct_def, ref ast_generics) => {
self.check_type_defn(item, true, |fcx| { check_type_defn(tcx, item, true, |fcx| {
vec![fcx.non_enum_variant(struct_def)] vec![fcx.non_enum_variant(struct_def)]
}); });
self.check_variances_for_type_defn(item, ast_generics); check_variances_for_type_defn(tcx, item, ast_generics);
} }
hir::ItemEnum(ref enum_def, ref ast_generics) => { hir::ItemEnum(ref enum_def, ref ast_generics) => {
self.check_type_defn(item, true, |fcx| { check_type_defn(tcx, item, true, |fcx| {
fcx.enum_variants(enum_def) fcx.enum_variants(enum_def)
}); });
self.check_variances_for_type_defn(item, ast_generics); check_variances_for_type_defn(tcx, item, ast_generics);
} }
hir::ItemTrait(..) => { hir::ItemTrait(..) => {
self.check_trait(item); check_trait(tcx, item);
} }
_ => {} _ => {}
} }
} }
fn check_associated_item(&mut self, pub fn check_trait_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
let trait_item = tcx.hir.expect_trait_item(node_id);
let method_sig = match trait_item.node {
hir::TraitItemKind::Method(ref sig, _) => Some(sig),
_ => None
};
check_associated_item(tcx, trait_item.id, trait_item.span, method_sig);
}
pub fn check_impl_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
let impl_item = tcx.hir.expect_impl_item(node_id);
let method_sig = match impl_item.node {
hir::ImplItemKind::Method(ref sig, _) => Some(sig),
_ => None
};
check_associated_item(tcx, impl_item.id, impl_item.span, method_sig);
}
fn check_associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item_id: ast::NodeId, item_id: ast::NodeId,
span: Span, span: Span,
sig_if_method: Option<&hir::MethodSig>) { 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| { for_id(tcx, item_id, span).with_fcx(|fcx, tcx| {
let item = fcx.tcx.associated_item(fcx.tcx.hir.local_def_id(item_id)); let item = fcx.tcx.associated_item(fcx.tcx.hir.local_def_id(item_id));
let (mut implied_bounds, self_ty) = match item.container { let (mut implied_bounds, self_ty) = match item.container {
@@ -185,10 +190,10 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
reject_shadowing_type_parameters(fcx.tcx, item.def_id); reject_shadowing_type_parameters(fcx.tcx, item.def_id);
let sig = fcx.tcx.fn_sig(item.def_id); let sig = fcx.tcx.fn_sig(item.def_id);
let sig = fcx.normalize_associated_types_in(span, &sig); let sig = fcx.normalize_associated_types_in(span, &sig);
this.check_fn_or_method(fcx, span, sig, check_fn_or_method(tcx, fcx, span, sig,
item.def_id, &mut implied_bounds); item.def_id, &mut implied_bounds);
let sig_if_method = sig_if_method.expect("bad signature for method"); let sig_if_method = sig_if_method.expect("bad signature for method");
this.check_method_receiver(fcx, sig_if_method, &item, self_ty); check_method_receiver(fcx, sig_if_method, &item, self_ty);
} }
ty::AssociatedKind::Type => { ty::AssociatedKind::Type => {
if item.defaultness.has_value() { if item.defaultness.has_value() {
@@ -203,28 +208,28 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
}) })
} }
fn for_item<'tcx>(&self, item: &hir::Item) fn for_item<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, item: &hir::Item)
-> CheckWfFcxBuilder<'a, 'gcx, 'tcx> { -> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
self.for_id(item.id, item.span) for_id(tcx, item.id, item.span)
} }
fn for_id<'tcx>(&self, id: ast::NodeId, span: Span) fn for_id<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>, id: ast::NodeId, span: Span)
-> CheckWfFcxBuilder<'a, 'gcx, 'tcx> { -> CheckWfFcxBuilder<'a, 'gcx, 'tcx> {
let def_id = self.tcx.hir.local_def_id(id); let def_id = tcx.hir.local_def_id(id);
CheckWfFcxBuilder { CheckWfFcxBuilder {
inherited: Inherited::build(self.tcx, def_id), inherited: Inherited::build(tcx, def_id),
code: self.code.clone(),
id, id,
span, span,
param_env: self.tcx.param_env(def_id), param_env: tcx.param_env(def_id),
} }
} }
/// In a type definition, we check that to ensure that the types of the fields are well-formed. /// In a type definition, we check that to ensure that the types of the fields are well-formed.
fn check_type_defn<F>(&mut self, item: &hir::Item, all_sized: bool, mut lookup_fields: F) fn check_type_defn<'a, 'tcx, F>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
where F: for<'fcx, 'tcx> FnMut(&FnCtxt<'fcx, 'gcx, 'tcx>) -> Vec<AdtVariant<'tcx>> item: &hir::Item, all_sized: bool, mut lookup_fields: F)
where F: for<'fcx, 'gcx, 'tcx2> FnMut(&FnCtxt<'fcx, 'gcx, 'tcx2>) -> Vec<AdtVariant<'tcx2>>
{ {
self.for_item(item).with_fcx(|fcx, this| { for_item(tcx, item).with_fcx(|fcx, fcx_tcx| {
let variants = lookup_fields(fcx); let variants = lookup_fields(fcx);
let def_id = fcx.tcx.hir.local_def_id(item.id); let def_id = fcx.tcx.hir.local_def_id(item.id);
let packed = fcx.tcx.adt_def(def_id).repr.packed(); let packed = fcx.tcx.adt_def(def_id).repr.packed();
@@ -235,11 +240,11 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
let needs_drop_copy = || { let needs_drop_copy = || {
packed && { packed && {
let ty = variant.fields.last().unwrap().ty; let ty = variant.fields.last().unwrap().ty;
let ty = fcx.tcx.erase_regions(&ty).lift_to_tcx(this.tcx) let ty = fcx.tcx.erase_regions(&ty).lift_to_tcx(fcx_tcx)
.unwrap_or_else(|| { .unwrap_or_else(|| {
span_bug!(item.span, "inference variables in {:?}", ty) span_bug!(item.span, "inference variables in {:?}", ty)
}); });
ty.needs_drop(this.tcx, this.tcx.param_env(def_id)) ty.needs_drop(fcx_tcx, fcx_tcx.param_env(def_id))
} }
}; };
let unsized_len = if let unsized_len = if
@@ -265,59 +270,60 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
// All field types must be well-formed. // All field types must be well-formed.
for field in &variant.fields { 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)
} }
} }
self.check_where_clauses(fcx, item.span, def_id); check_where_clauses(tcx, fcx, item.span, def_id);
vec![] // no implied bounds in a struct def'n vec![] // no implied bounds in a struct def'n
}); });
} }
fn check_trait(&mut self, item: &hir::Item) { fn check_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
let trait_def_id = self.tcx.hir.local_def_id(item.id); let trait_def_id = tcx.hir.local_def_id(item.id);
self.for_item(item).with_fcx(|fcx, _| { for_item(tcx, item).with_fcx(|fcx, _| {
self.check_where_clauses(fcx, item.span, trait_def_id); check_where_clauses(tcx, fcx, item.span, trait_def_id);
vec![] vec![]
}); });
} }
fn check_item_fn(&mut self, item: &hir::Item) { fn check_item_fn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &hir::Item) {
self.for_item(item).with_fcx(|fcx, this| { for_item(tcx, item).with_fcx(|fcx, tcx| {
let def_id = fcx.tcx.hir.local_def_id(item.id); let def_id = fcx.tcx.hir.local_def_id(item.id);
let sig = fcx.tcx.fn_sig(def_id); let sig = fcx.tcx.fn_sig(def_id);
let sig = fcx.normalize_associated_types_in(item.span, &sig); let sig = fcx.normalize_associated_types_in(item.span, &sig);
let mut implied_bounds = vec![]; let mut implied_bounds = vec![];
this.check_fn_or_method(fcx, item.span, sig, check_fn_or_method(tcx, fcx, item.span, sig,
def_id, &mut implied_bounds); def_id, &mut implied_bounds);
implied_bounds implied_bounds
}) })
} }
fn check_item_type(&mut self, fn check_item_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item: &hir::Item) item: &hir::Item)
{ {
debug!("check_item_type: {:?}", item); debug!("check_item_type: {:?}", item);
self.for_item(item).with_fcx(|fcx, this| { for_item(tcx, item).with_fcx(|fcx, _this| {
let ty = fcx.tcx.type_of(fcx.tcx.hir.local_def_id(item.id)); 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); 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 vec![] // no implied bounds in a const etc
}); });
} }
fn check_impl(&mut self, fn check_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item: &hir::Item, item: &hir::Item,
ast_self_ty: &hir::Ty, ast_self_ty: &hir::Ty,
ast_trait_ref: &Option<hir::TraitRef>) ast_trait_ref: &Option<hir::TraitRef>)
{ {
debug!("check_impl: {:?}", item); debug!("check_impl: {:?}", item);
self.for_item(item).with_fcx(|fcx, this| { for_item(tcx, item).with_fcx(|fcx, tcx| {
let item_def_id = fcx.tcx.hir.local_def_id(item.id); let item_def_id = fcx.tcx.hir.local_def_id(item.id);
match *ast_trait_ref { match *ast_trait_ref {
@@ -339,18 +345,19 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
None => { None => {
let self_ty = fcx.tcx.type_of(item_def_id); let self_ty = fcx.tcx.type_of(item_def_id);
let self_ty = fcx.normalize_associated_types_in(item.span, &self_ty); 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);
} }
} }
this.check_where_clauses(fcx, item.span, item_def_id); check_where_clauses(tcx, fcx, item.span, item_def_id);
fcx.impl_implied_bounds(item_def_id, item.span) fcx.impl_implied_bounds(item_def_id, item.span)
}); });
} }
/// Checks where clauses and inline bounds that are declared on def_id. /// Checks where clauses and inline bounds that are declared on def_id.
fn check_where_clauses<'fcx, 'tcx>(&mut self, fn check_where_clauses<'a, 'gcx, 'fcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
fcx: &FnCtxt<'fcx, 'gcx, 'tcx>, fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
span: Span, span: Span,
def_id: DefId) { def_id: DefId) {
@@ -360,7 +367,7 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
let mut predicates = fcx.tcx.predicates_of(def_id); let mut predicates = fcx.tcx.predicates_of(def_id);
let mut substituted_predicates = Vec::new(); let mut substituted_predicates = Vec::new();
let generics = self.tcx.generics_of(def_id); let generics = tcx.generics_of(def_id);
let is_our_default = |def: &ty::TypeParameterDef| let is_our_default = |def: &ty::TypeParameterDef|
def.has_default && def.index >= generics.parent_count() as u32; def.has_default && def.index >= generics.parent_count() as u32;
@@ -374,7 +381,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
// parameter includes another (e.g., <T, U = T>). In those cases, we can't // 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. // be sure if it will error or not as user might always specify the other.
if !ty.needs_subst() { 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);
} }
} }
@@ -447,7 +455,7 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
} }
} }
fn check_fn_or_method<'fcx, 'tcx>(&mut self, fn check_fn_or_method<'a, 'fcx, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
fcx: &FnCtxt<'fcx, 'gcx, 'tcx>, fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
span: Span, span: Span,
sig: ty::PolyFnSig<'tcx>, sig: ty::PolyFnSig<'tcx>,
@@ -458,20 +466,19 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
let sig = fcx.tcx.liberate_late_bound_regions(def_id, &sig); let sig = fcx.tcx.liberate_late_bound_regions(def_id, &sig);
for input_ty in sig.inputs() { 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()); 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 // FIXME(#25759) return types should not be implied bounds
implied_bounds.push(sig.output()); implied_bounds.push(sig.output());
self.check_where_clauses(fcx, span, def_id); check_where_clauses(tcx, fcx, span, def_id);
} }
fn check_method_receiver<'fcx, 'tcx>(&mut self, fn check_method_receiver<'fcx, 'gcx, 'tcx>(fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
method_sig: &hir::MethodSig, method_sig: &hir::MethodSig,
method: &ty::AssociatedItem, method: &ty::AssociatedItem,
self_ty: Ty<'tcx>) self_ty: Ty<'tcx>)
@@ -566,19 +573,19 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
} }
} }
fn check_variances_for_type_defn(&self, fn check_variances_for_type_defn<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
item: &hir::Item, item: &hir::Item,
ast_generics: &hir::Generics) ast_generics: &hir::Generics)
{ {
let item_def_id = self.tcx.hir.local_def_id(item.id); let item_def_id = tcx.hir.local_def_id(item.id);
let ty = self.tcx.type_of(item_def_id); let ty = tcx.type_of(item_def_id);
if self.tcx.has_error_field(ty) { if tcx.has_error_field(ty) {
return; return;
} }
let ty_predicates = self.tcx.predicates_of(item_def_id); let ty_predicates = tcx.predicates_of(item_def_id);
assert_eq!(ty_predicates.parent, None); assert_eq!(ty_predicates.parent, None);
let variances = self.tcx.variances_of(item_def_id); let variances = tcx.variances_of(item_def_id);
let mut constrained_parameters: FxHashSet<_> = let mut constrained_parameters: FxHashSet<_> =
variances.iter().enumerate() variances.iter().enumerate()
@@ -586,7 +593,7 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
.map(|(index, _)| Parameter(index as u32)) .map(|(index, _)| Parameter(index as u32))
.collect(); .collect();
identify_constrained_type_params(self.tcx, identify_constrained_type_params(tcx,
ty_predicates.predicates.as_slice(), ty_predicates.predicates.as_slice(),
None, None,
&mut constrained_parameters); &mut constrained_parameters);
@@ -600,23 +607,23 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
hir::GenericParam::Lifetime(ref ld) => (ld.lifetime.span, ld.lifetime.name.name()), hir::GenericParam::Lifetime(ref ld) => (ld.lifetime.span, ld.lifetime.name.name()),
hir::GenericParam::Type(ref tp) => (tp.span, tp.name), hir::GenericParam::Type(ref tp) => (tp.span, tp.name),
}; };
self.report_bivariance(span, name); report_bivariance(tcx, span, name);
} }
} }
fn report_bivariance(&self, fn report_bivariance<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
span: Span, span: Span,
param_name: ast::Name) param_name: ast::Name)
{ {
let mut err = error_392(self.tcx, span, param_name); let mut err = error_392(tcx, span, param_name);
let suggested_marker_id = self.tcx.lang_items().phantom_data(); let suggested_marker_id = tcx.lang_items().phantom_data();
match suggested_marker_id { match suggested_marker_id {
Some(def_id) => { Some(def_id) => {
err.help( err.help(
&format!("consider removing `{}` or using a marker such as `{}`", &format!("consider removing `{}` or using a marker such as `{}`",
param_name, param_name,
self.tcx.item_path_str(def_id))); tcx.item_path_str(def_id)));
} }
None => { None => {
// no lang items, no help! // no lang items, no help!
@@ -624,7 +631,6 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
} }
err.emit(); err.emit();
} }
}
fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) { fn reject_shadowing_type_parameters(tcx: TyCtxt, def_id: DefId) {
let generics = tcx.generics_of(def_id); let generics = tcx.generics_of(def_id);
@@ -648,6 +654,19 @@ 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> { impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> { fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'v> {
NestedVisitorMap::None NestedVisitorMap::None
@@ -655,27 +674,22 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'a, 'tcx> {
fn visit_item(&mut self, i: &hir::Item) { fn visit_item(&mut self, i: &hir::Item) {
debug!("visit_item: {:?}", i); 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); intravisit::walk_item(self, i);
} }
fn visit_trait_item(&mut self, trait_item: &'v hir::TraitItem) { fn visit_trait_item(&mut self, trait_item: &'v hir::TraitItem) {
debug!("visit_trait_item: {:?}", trait_item); debug!("visit_trait_item: {:?}", trait_item);
let method_sig = match trait_item.node { let def_id = self.tcx.hir.local_def_id(trait_item.id);
hir::TraitItemKind::Method(ref sig, _) => Some(sig), ty::maps::queries::check_trait_item_well_formed::ensure(self.tcx, def_id);
_ => None
};
self.check_associated_item(trait_item.id, trait_item.span, method_sig);
intravisit::walk_trait_item(self, trait_item) intravisit::walk_trait_item(self, trait_item)
} }
fn visit_impl_item(&mut self, impl_item: &'v hir::ImplItem) { fn visit_impl_item(&mut self, impl_item: &'v hir::ImplItem) {
debug!("visit_impl_item: {:?}", impl_item); debug!("visit_impl_item: {:?}", impl_item);
let method_sig = match impl_item.node { let def_id = self.tcx.hir.local_def_id(impl_item.id);
hir::ImplItemKind::Method(ref sig, _) => Some(sig), ty::maps::queries::check_impl_item_well_formed::ensure(self.tcx, def_id);
_ => None
};
self.check_associated_item(impl_item.id, impl_item.span, method_sig);
intravisit::walk_impl_item(self, impl_item) intravisit::walk_impl_item(self, impl_item)
} }
} }