Skip to content

Commit 3790f08

Browse files
committed
Fix tcx.environment for inherent impls
1 parent 8e2bdaa commit 3790f08

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

src/librustc_traits/lowering/environment.rs

+48-31
Original file line numberDiff line numberDiff line change
@@ -216,56 +216,73 @@ crate fn environment<'a, 'tcx>(
216216
let node_id = tcx.hir().as_local_node_id(def_id).unwrap();
217217
let node = tcx.hir().get(node_id);
218218

219-
let mut is_fn = false;
220-
let mut is_impl = false;
221-
match node {
219+
enum NodeKind {
220+
TraitImpl,
221+
InherentImpl,
222+
Fn,
223+
Other,
224+
};
225+
226+
let node_kind = match node {
222227
Node::TraitItem(item) => match item.node {
223-
TraitItemKind::Method(..) => is_fn = true,
224-
_ => (),
228+
TraitItemKind::Method(..) => NodeKind::Fn,
229+
_ => NodeKind::Other,
225230
}
226231

227232
Node::ImplItem(item) => match item.node {
228-
ImplItemKind::Method(..) => is_fn = true,
229-
_ => (),
233+
ImplItemKind::Method(..) => NodeKind::Fn,
234+
_ => NodeKind::Other,
230235
}
231236

232237
Node::Item(item) => match item.node {
233-
ItemKind::Impl(..) => is_impl = true,
234-
ItemKind::Fn(..) => is_fn = true,
235-
_ => (),
238+
ItemKind::Impl(.., Some(..), _, _) => NodeKind::TraitImpl,
239+
ItemKind::Impl(.., None, _, _) => NodeKind::InherentImpl,
240+
ItemKind::Fn(..) => NodeKind::Fn,
241+
_ => NodeKind::Other,
236242
}
237243

238244
Node::ForeignItem(item) => match item.node {
239-
ForeignItemKind::Fn(..) => is_fn = true,
240-
_ => (),
245+
ForeignItemKind::Fn(..) => NodeKind::Fn,
246+
_ => NodeKind::Other,
241247
}
242248

243249
// FIXME: closures?
244-
_ => (),
245-
}
250+
_ => NodeKind::Other,
251+
};
246252

247253
let mut input_tys = FxHashSet::default();
248254

249-
// In an impl, we assume that the header trait ref and all its constituents
250-
// are well-formed.
251-
if is_impl {
252-
let trait_ref = tcx.impl_trait_ref(def_id)
253-
.expect("not an impl");
255+
match node_kind {
256+
// In a trait impl, we assume that the header trait ref and all its
257+
// constituents are well-formed.
258+
NodeKind::TraitImpl => {
259+
let trait_ref = tcx.impl_trait_ref(def_id)
260+
.expect("not an impl");
254261

255-
input_tys.extend(
256-
trait_ref.input_types().flat_map(|ty| ty.walk())
257-
);
258-
}
262+
input_tys.extend(
263+
trait_ref.input_types().flat_map(|ty| ty.walk())
264+
);
265+
}
259266

260-
// In an fn, we assume that the arguments and all their constituents are
261-
// well-formed.
262-
if is_fn {
263-
let fn_sig = tcx.fn_sig(def_id);
264-
let fn_sig = tcx.liberate_late_bound_regions(def_id, &fn_sig);
267+
// In an inherent impl, we assume that the receiver type and all its
268+
// constituents are well-formed.
269+
NodeKind::InherentImpl => {
270+
let self_ty = tcx.type_of(def_id);
271+
input_tys.extend(self_ty.walk());
272+
}
265273

266-
input_tys.extend(
267-
fn_sig.inputs().iter().flat_map(|ty| ty.walk())
268-
);
274+
// In an fn, we assume that the arguments and all their constituents are
275+
// well-formed.
276+
NodeKind::Fn => {
277+
let fn_sig = tcx.fn_sig(def_id);
278+
let fn_sig = tcx.liberate_late_bound_regions(def_id, &fn_sig);
279+
280+
input_tys.extend(
281+
fn_sig.inputs().iter().flat_map(|ty| ty.walk())
282+
);
283+
}
284+
285+
NodeKind::Other => (),
269286
}
270287

271288
let clauses = clauses.chain(

0 commit comments

Comments
 (0)