Skip to content

Commit c17be9e

Browse files
committed
move impl wf check so they occur earlier
Needed to keep coherence from freaking out.
1 parent 34c361c commit c17be9e

File tree

5 files changed

+92
-69
lines changed

5 files changed

+92
-69
lines changed

src/librustc_typeck/check/impl_item_duplicate.rs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ mod closure;
143143
mod callee;
144144
mod compare_method;
145145
mod intrinsic;
146-
mod impl_item_duplicate;
147-
mod impl_parameters_used;
148146
mod op;
149147

150148
/// closures defined within the function. For example:
@@ -817,7 +815,7 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
817815
it.id);
818816
}
819817
hir::ItemFn(..) => {} // entirely within check_item_body
820-
hir::ItemImpl(_, _, ref hir_generics, _, _, ref impl_item_refs) => {
818+
hir::ItemImpl(.., ref impl_item_refs) => {
821819
debug!("ItemImpl {} with id {}", it.name, it.id);
822820
let impl_def_id = ccx.tcx.map.local_def_id(it.id);
823821
if let Some(impl_trait_ref) = ccx.tcx.impl_trait_ref(impl_def_id) {
@@ -829,14 +827,6 @@ pub fn check_item_type<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>, it: &'tcx hir::Item) {
829827
let trait_def_id = impl_trait_ref.def_id;
830828
check_on_unimplemented(ccx, trait_def_id, it);
831829
}
832-
833-
impl_parameters_used::enforce_impl_params_are_constrained(ccx,
834-
hir_generics,
835-
impl_def_id,
836-
impl_item_refs);
837-
838-
impl_item_duplicate::enforce_impl_items_are_distinct(ccx,
839-
impl_item_refs);
840830
}
841831
hir::ItemTrait(..) => {
842832
let def_id = ccx.tcx.map.local_def_id(it.id);

src/librustc_typeck/check/impl_parameters_used.rs renamed to src/librustc_typeck/impl_wf_check.rs

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! This pass enforces various "well-formedness constraints" on impls.
12+
//! Logically, it is part of wfcheck -- but we do it early so that we
13+
//! can stop compilation afterwards, since part of the trait matching
14+
//! infrastructure gets very grumpy if these conditions don't hold. In
15+
//! particular, if there are type parameters that are not part of the
16+
//! impl, then coherence will report strange inference ambiguity
17+
//! errors; if impls have duplicate items, we get misleading
18+
//! specialization errors. These things can (and probably should) be
19+
//! fixed, but for the moment it's easier to do these checks early.
20+
1121
use constrained_type_params as ctp;
22+
use rustc::dep_graph::DepNode;
1223
use rustc::hir;
24+
use rustc::hir::itemlikevisit::ItemLikeVisitor;
1325
use rustc::hir::def_id::DefId;
1426
use rustc::ty;
15-
use rustc::util::nodemap::FxHashSet;
27+
use rustc::util::nodemap::{FxHashMap, FxHashSet};
28+
use std::collections::hash_map::Entry::{Occupied, Vacant};
1629

1730
use syntax_pos::Span;
1831

@@ -48,22 +61,52 @@ use CrateCtxt;
4861
/// impl<'a> Trait<Foo> for Bar { type X = &'a i32; }
4962
/// ^ 'a is unused and appears in assoc type, error
5063
/// ```
51-
pub fn enforce_impl_params_are_constrained<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
52-
impl_hir_generics: &hir::Generics,
53-
impl_def_id: DefId,
54-
impl_item_refs: &[hir::ImplItemRef])
64+
pub fn impl_wf_check<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>) {
65+
// We will tag this as part of the WF check -- logically, it is,
66+
// but it's one that we must perform earlier than the rest of
67+
// WfCheck.
68+
ccx.tcx.visit_all_item_likes_in_krate(DepNode::WfCheck, &mut ImplWfCheck { ccx: ccx });
69+
}
70+
71+
struct ImplWfCheck<'a, 'tcx: 'a> {
72+
ccx: &'a CrateCtxt<'a, 'tcx>,
73+
}
74+
75+
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
76+
fn visit_item(&mut self, item: &'tcx hir::Item) {
77+
match item.node {
78+
hir::ItemImpl(.., ref generics, _, _, ref impl_item_refs) => {
79+
let impl_def_id = self.ccx.tcx.map.local_def_id(item.id);
80+
enforce_impl_params_are_constrained(self.ccx,
81+
generics,
82+
impl_def_id,
83+
impl_item_refs);
84+
enforce_impl_items_are_distinct(self.ccx, impl_item_refs);
85+
}
86+
_ => { }
87+
}
88+
}
89+
90+
fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) { }
91+
}
92+
93+
fn enforce_impl_params_are_constrained<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
94+
impl_hir_generics: &hir::Generics,
95+
impl_def_id: DefId,
96+
impl_item_refs: &[hir::ImplItemRef])
5597
{
5698
// Every lifetime used in an associated type must be constrained.
57-
let impl_scheme = ccx.tcx.lookup_item_type(impl_def_id);
58-
let impl_predicates = ccx.tcx.lookup_predicates(impl_def_id);
99+
let impl_self_ty = ccx.tcx.item_type(impl_def_id);
100+
let impl_generics = ccx.tcx.item_generics(impl_def_id);
101+
let impl_predicates = ccx.tcx.item_predicates(impl_def_id);
59102
let impl_trait_ref = ccx.tcx.impl_trait_ref(impl_def_id);
60103

61-
let mut input_parameters = ctp::parameters_for_impl(impl_scheme.ty, impl_trait_ref);
104+
let mut input_parameters = ctp::parameters_for_impl(impl_self_ty, impl_trait_ref);
62105
ctp::identify_constrained_type_params(
63106
&impl_predicates.predicates.as_slice(), impl_trait_ref, &mut input_parameters);
64107

65108
// Disallow ANY unconstrained type parameters.
66-
for (ty_param, param) in impl_scheme.generics.types.iter().zip(&impl_hir_generics.ty_params) {
109+
for (ty_param, param) in impl_generics.types.iter().zip(&impl_hir_generics.ty_params) {
67110
let param_ty = ty::ParamTy::for_def(ty_param);
68111
if !input_parameters.contains(&ctp::Parameter::from(param_ty)) {
69112
report_unused_parameter(ccx, param.span, "type", &param_ty.to_string());
@@ -78,9 +121,9 @@ pub fn enforce_impl_params_are_constrained<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
78121
item.kind == ty::AssociatedKind::Type && item.has_value
79122
})
80123
.flat_map(|def_id| {
81-
ctp::parameters_for(&ccx.tcx.lookup_item_type(def_id).ty, true)
124+
ctp::parameters_for(&ccx.tcx.item_type(def_id), true)
82125
}).collect();
83-
for (ty_lifetime, lifetime) in impl_scheme.generics.regions.iter()
126+
for (ty_lifetime, lifetime) in impl_generics.regions.iter()
84127
.zip(&impl_hir_generics.lifetimes)
85128
{
86129
let param = ctp::Parameter::from(ty_lifetime.to_early_bound_region_data());
@@ -127,3 +170,34 @@ fn report_unused_parameter(ccx: &CrateCtxt,
127170
.span_label(span, &format!("unconstrained {} parameter", kind))
128171
.emit();
129172
}
173+
174+
/// Enforce that we do not have two items in an impl with the same name.
175+
fn enforce_impl_items_are_distinct<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
176+
impl_item_refs: &[hir::ImplItemRef])
177+
{
178+
let tcx = ccx.tcx;
179+
let mut seen_type_items = FxHashMap();
180+
let mut seen_value_items = FxHashMap();
181+
for impl_item_ref in impl_item_refs {
182+
let impl_item = tcx.map.impl_item(impl_item_ref.id);
183+
let seen_items = match impl_item.node {
184+
hir::ImplItemKind::Type(_) => &mut seen_type_items,
185+
_ => &mut seen_value_items,
186+
};
187+
match seen_items.entry(impl_item.name) {
188+
Occupied(entry) => {
189+
let mut err = struct_span_err!(tcx.sess, impl_item.span, E0201,
190+
"duplicate definitions with name `{}`:",
191+
impl_item.name);
192+
err.span_label(*entry.get(),
193+
&format!("previous definition of `{}` here",
194+
impl_item.name));
195+
err.span_label(impl_item.span, &format!("duplicate definition"));
196+
err.emit();
197+
}
198+
Vacant(entry) => {
199+
entry.insert(impl_item.span);
200+
}
201+
}
202+
}
203+
}

src/librustc_typeck/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ mod rscope;
131131
mod astconv;
132132
pub mod collect;
133133
mod constrained_type_params;
134+
mod impl_wf_check;
134135
pub mod coherence;
135136
pub mod variance;
136137

@@ -334,6 +335,11 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
334335
time(time_passes, "variance inference", ||
335336
variance::infer_variance(tcx));
336337

338+
tcx.sess.track_errors(|| {
339+
time(time_passes, "impl wf inference", ||
340+
impl_wf_check::impl_wf_check(&ccx));
341+
})?;
342+
337343
tcx.sess.track_errors(|| {
338344
time(time_passes, "coherence checking", ||
339345
coherence::check_coherence(&ccx));

src/test/compile-fail/issue-3214.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ fn foo<T>() {
1515

1616
impl<T> Drop for foo<T> {
1717
//~^ ERROR wrong number of type arguments
18-
//~^^ ERROR the type parameter `T` is not constrained
1918
fn drop(&mut self) {}
2019
}
2120
}

0 commit comments

Comments
 (0)