]> git.proxmox.com Git - rustc.git/blame - src/librustc_typeck/impl_wf_check.rs
New upstream version 1.20.0+dfsg1
[rustc.git] / src / librustc_typeck / impl_wf_check.rs
CommitLineData
476ff2be
SL
1// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
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
21use constrained_type_params as ctp;
476ff2be
SL
22use rustc::hir;
23use rustc::hir::itemlikevisit::ItemLikeVisitor;
24use rustc::hir::def_id::DefId;
8bb4bdeb 25use rustc::ty::{self, TyCtxt};
476ff2be
SL
26use rustc::util::nodemap::{FxHashMap, FxHashSet};
27use std::collections::hash_map::Entry::{Occupied, Vacant};
28
29use syntax_pos::Span;
30
476ff2be
SL
31/// Checks that all the type/lifetime parameters on an impl also
32/// appear in the trait ref or self-type (or are constrained by a
33/// where-clause). These rules are needed to ensure that, given a
34/// trait ref like `<T as Trait<U>>`, we can derive the values of all
35/// parameters on the impl (which is needed to make specialization
36/// possible).
37///
38/// However, in the case of lifetimes, we only enforce these rules if
39/// the lifetime parameter is used in an associated type. This is a
40/// concession to backwards compatibility; see comment at the end of
41/// the fn for details.
42///
43/// Example:
44///
45/// ```
46/// impl<T> Trait<Foo> for Bar { ... }
47/// ^ T does not appear in `Foo` or `Bar`, error!
48///
49/// impl<T> Trait<Foo<T>> for Bar { ... }
50/// ^ T appears in `Foo<T>`, ok.
51///
52/// impl<T> Trait<Foo> for Bar where Bar: Iterator<Item=T> { ... }
53/// ^ T is bound to `<Bar as Iterator>::Item`, ok.
54///
55/// impl<'a> Trait<Foo> for Bar { }
56/// ^ 'a is unused, but for back-compat we allow it
57///
58/// impl<'a> Trait<Foo> for Bar { type X = &'a i32; }
59/// ^ 'a is unused and appears in assoc type, error
60/// ```
8bb4bdeb 61pub fn impl_wf_check<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
476ff2be
SL
62 // We will tag this as part of the WF check -- logically, it is,
63 // but it's one that we must perform earlier than the rest of
64 // WfCheck.
cc61c64b 65 tcx.hir.krate().visit_all_item_likes(&mut ImplWfCheck { tcx: tcx });
476ff2be
SL
66}
67
68struct ImplWfCheck<'a, 'tcx: 'a> {
8bb4bdeb 69 tcx: TyCtxt<'a, 'tcx, 'tcx>,
476ff2be
SL
70}
71
72impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
73 fn visit_item(&mut self, item: &'tcx hir::Item) {
74 match item.node {
75 hir::ItemImpl(.., ref generics, _, _, ref impl_item_refs) => {
8bb4bdeb
XL
76 let impl_def_id = self.tcx.hir.local_def_id(item.id);
77 enforce_impl_params_are_constrained(self.tcx,
476ff2be
SL
78 generics,
79 impl_def_id,
80 impl_item_refs);
8bb4bdeb 81 enforce_impl_items_are_distinct(self.tcx, impl_item_refs);
476ff2be
SL
82 }
83 _ => { }
84 }
85 }
86
32a655c1
SL
87 fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem) { }
88
476ff2be
SL
89 fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) { }
90}
91
8bb4bdeb 92fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
476ff2be
SL
93 impl_hir_generics: &hir::Generics,
94 impl_def_id: DefId,
95 impl_item_refs: &[hir::ImplItemRef])
96{
97 // Every lifetime used in an associated type must be constrained.
7cac9316
XL
98 let impl_self_ty = tcx.type_of(impl_def_id);
99 let impl_generics = tcx.generics_of(impl_def_id);
100 let impl_predicates = tcx.predicates_of(impl_def_id);
8bb4bdeb 101 let impl_trait_ref = tcx.impl_trait_ref(impl_def_id);
476ff2be
SL
102
103 let mut input_parameters = ctp::parameters_for_impl(impl_self_ty, impl_trait_ref);
104 ctp::identify_constrained_type_params(
041b39d2 105 tcx, &impl_predicates.predicates.as_slice(), impl_trait_ref, &mut input_parameters);
476ff2be
SL
106
107 // Disallow ANY unconstrained type parameters.
108 for (ty_param, param) in impl_generics.types.iter().zip(&impl_hir_generics.ty_params) {
109 let param_ty = ty::ParamTy::for_def(ty_param);
110 if !input_parameters.contains(&ctp::Parameter::from(param_ty)) {
8bb4bdeb 111 report_unused_parameter(tcx, param.span, "type", &param_ty.to_string());
476ff2be
SL
112 }
113 }
114
115 // Disallow unconstrained lifetimes, but only if they appear in assoc types.
116 let lifetimes_in_associated_types: FxHashSet<_> = impl_item_refs.iter()
8bb4bdeb 117 .map(|item_ref| tcx.hir.local_def_id(item_ref.id.node_id))
476ff2be 118 .filter(|&def_id| {
8bb4bdeb 119 let item = tcx.associated_item(def_id);
476ff2be
SL
120 item.kind == ty::AssociatedKind::Type && item.defaultness.has_value()
121 })
122 .flat_map(|def_id| {
7cac9316 123 ctp::parameters_for(&tcx.type_of(def_id), true)
476ff2be
SL
124 }).collect();
125 for (ty_lifetime, lifetime) in impl_generics.regions.iter()
126 .zip(&impl_hir_generics.lifetimes)
127 {
128 let param = ctp::Parameter::from(ty_lifetime.to_early_bound_region_data());
129
130 if
131 lifetimes_in_associated_types.contains(&param) && // (*)
132 !input_parameters.contains(&param)
133 {
8bb4bdeb 134 report_unused_parameter(tcx, lifetime.lifetime.span,
476ff2be
SL
135 "lifetime", &lifetime.lifetime.name.to_string());
136 }
137 }
138
139 // (*) This is a horrible concession to reality. I think it'd be
140 // better to just ban unconstrianed lifetimes outright, but in
141 // practice people do non-hygenic macros like:
142 //
143 // ```
144 // macro_rules! __impl_slice_eq1 {
145 // ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
146 // impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
147 // ....
148 // }
149 // }
150 // }
151 // ```
152 //
153 // In a concession to backwards compatbility, we continue to
154 // permit those, so long as the lifetimes aren't used in
155 // associated types. I believe this is sound, because lifetimes
156 // used elsewhere are not projected back out.
157}
158
8bb4bdeb 159fn report_unused_parameter(tcx: TyCtxt,
476ff2be
SL
160 span: Span,
161 kind: &str,
162 name: &str)
163{
164 struct_span_err!(
8bb4bdeb 165 tcx.sess, span, E0207,
476ff2be
SL
166 "the {} parameter `{}` is not constrained by the \
167 impl trait, self type, or predicates",
168 kind, name)
7cac9316 169 .span_label(span, format!("unconstrained {} parameter", kind))
476ff2be
SL
170 .emit();
171}
172
173/// Enforce that we do not have two items in an impl with the same name.
8bb4bdeb 174fn enforce_impl_items_are_distinct<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
476ff2be
SL
175 impl_item_refs: &[hir::ImplItemRef])
176{
476ff2be
SL
177 let mut seen_type_items = FxHashMap();
178 let mut seen_value_items = FxHashMap();
179 for impl_item_ref in impl_item_refs {
32a655c1 180 let impl_item = tcx.hir.impl_item(impl_item_ref.id);
476ff2be
SL
181 let seen_items = match impl_item.node {
182 hir::ImplItemKind::Type(_) => &mut seen_type_items,
183 _ => &mut seen_value_items,
184 };
185 match seen_items.entry(impl_item.name) {
186 Occupied(entry) => {
187 let mut err = struct_span_err!(tcx.sess, impl_item.span, E0201,
188 "duplicate definitions with name `{}`:",
189 impl_item.name);
190 err.span_label(*entry.get(),
7cac9316 191 format!("previous definition of `{}` here",
476ff2be 192 impl_item.name));
7cac9316 193 err.span_label(impl_item.span, "duplicate definition");
476ff2be
SL
194 err.emit();
195 }
196 Vacant(entry) => {
197 entry.insert(impl_item.span);
198 }
199 }
200 }
201}