1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::constrained_generic_params as cgp;
use min_specialization::check_min_specialization;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::struct_span_err;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeVisitable};
use rustc_span::{Span, Symbol};
use std::collections::hash_map::Entry::{Occupied, Vacant};
mod min_specialization;
fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
let min_specialization = tcx.features().min_specialization;
let module = tcx.hir_module_items(module_def_id);
for id in module.items() {
if matches!(tcx.def_kind(id.def_id), DefKind::Impl) {
enforce_impl_params_are_constrained(tcx, id.def_id);
enforce_impl_items_are_distinct(tcx, id.def_id);
if min_specialization {
check_min_specialization(tcx, id.def_id);
}
}
}
}
pub fn provide(providers: &mut Providers) {
*providers = Providers { check_mod_impl_wf, ..*providers };
}
fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) {
let impl_self_ty = tcx.type_of(impl_def_id);
if impl_self_ty.references_error() {
tcx.sess.delay_span_bug(
tcx.def_span(impl_def_id),
&format!(
"potentially unconstrained type parameters weren't evaluated: {:?}",
impl_self_ty,
),
);
return;
}
let impl_generics = tcx.generics_of(impl_def_id);
let impl_predicates = tcx.predicates_of(impl_def_id);
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id);
let mut input_parameters = cgp::parameters_for_impl(impl_self_ty, impl_trait_ref);
cgp::identify_constrained_generic_params(
tcx,
impl_predicates,
impl_trait_ref,
&mut input_parameters,
);
let lifetimes_in_associated_types: FxHashSet<_> = tcx
.associated_item_def_ids(impl_def_id)
.iter()
.flat_map(|def_id| {
let item = tcx.associated_item(def_id);
match item.kind {
ty::AssocKind::Type => {
if item.defaultness(tcx).has_value() {
cgp::parameters_for(&tcx.type_of(def_id), true)
} else {
Vec::new()
}
}
ty::AssocKind::Fn | ty::AssocKind::Const => Vec::new(),
}
})
.collect();
for param in &impl_generics.params {
match param.kind {
ty::GenericParamDefKind::Type { .. } => {
let param_ty = ty::ParamTy::for_def(param);
if !input_parameters.contains(&cgp::Parameter::from(param_ty)) {
report_unused_parameter(tcx, tcx.def_span(param.def_id), "type", param_ty.name);
}
}
ty::GenericParamDefKind::Lifetime => {
let param_lt = cgp::Parameter::from(param.to_early_bound_region_data());
if lifetimes_in_associated_types.contains(¶m_lt) && !input_parameters.contains(¶m_lt)
{
report_unused_parameter(
tcx,
tcx.def_span(param.def_id),
"lifetime",
param.name,
);
}
}
ty::GenericParamDefKind::Const { .. } => {
let param_ct = ty::ParamConst::for_def(param);
if !input_parameters.contains(&cgp::Parameter::from(param_ct)) {
report_unused_parameter(
tcx,
tcx.def_span(param.def_id),
"const",
param_ct.name,
);
}
}
}
}
}
fn report_unused_parameter(tcx: TyCtxt<'_>, span: Span, kind: &str, name: Symbol) {
let mut err = struct_span_err!(
tcx.sess,
span,
E0207,
"the {} parameter `{}` is not constrained by the \
impl trait, self type, or predicates",
kind,
name
);
err.span_label(span, format!("unconstrained {} parameter", kind));
if kind == "const" {
err.note(
"expressions using a const parameter must map each value to a distinct output value",
);
err.note(
"proving the result of expressions other than the parameter are unique is not supported",
);
}
err.emit();
}
fn enforce_impl_items_are_distinct(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) {
let mut seen_type_items = FxHashMap::default();
let mut seen_value_items = FxHashMap::default();
for &impl_item_ref in tcx.associated_item_def_ids(impl_def_id) {
let impl_item = tcx.associated_item(impl_item_ref);
let seen_items = match impl_item.kind {
ty::AssocKind::Type => &mut seen_type_items,
_ => &mut seen_value_items,
};
let span = tcx.def_span(impl_item_ref);
let ident = impl_item.ident(tcx);
match seen_items.entry(ident.normalize_to_macros_2_0()) {
Occupied(entry) => {
let mut err = struct_span_err!(
tcx.sess,
span,
E0201,
"duplicate definitions with name `{}`:",
ident
);
err.span_label(*entry.get(), format!("previous definition of `{}` here", ident));
err.span_label(span, "duplicate definition");
err.emit();
}
Vacant(entry) => {
entry.insert(span);
}
}
}
}