Skip to content

Commit 973ce01

Browse files
committed
Perform unconstrained param detection in explicit_predicates_of and taint the query result
1 parent b6b72c7 commit 973ce01

File tree

15 files changed

+96
-46
lines changed

15 files changed

+96
-46
lines changed

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use rustc_hir as hir;
44
use rustc_hir::def::DefKind;
55
use rustc_hir::def_id::{DefId, LocalDefId};
66
use rustc_hir::intravisit::{self, Visitor};
7-
use rustc_middle::ty::{self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, Upcast};
7+
use rustc_middle::ty::{
8+
self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitableExt, Upcast,
9+
};
810
use rustc_middle::{bug, span_bug};
911
use rustc_span::symbol::Ident;
1012
use rustc_span::{Span, DUMMY_SP};
@@ -86,6 +88,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
8688
parent: Some(tcx.parent(def_id.to_def_id())),
8789
predicates: tcx.arena.alloc_from_iter(predicates),
8890
effects_min_tys: ty::List::empty(),
91+
errored_due_to_unconstrained_params: Ok(()),
8992
};
9093
}
9194

@@ -108,6 +111,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
108111
parent: Some(impl_def_id),
109112
predicates: tcx.arena.alloc_from_iter(impl_predicates),
110113
effects_min_tys: ty::List::empty(),
114+
errored_due_to_unconstrained_params: trait_assoc_predicates
115+
.errored_due_to_unconstrained_params,
111116
};
112117
}
113118

@@ -128,6 +133,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
128133
// Preserving the order of insertion is important here so as not to break UI tests.
129134
let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
130135
let mut effects_min_tys = Vec::new();
136+
let mut errored_due_to_unconstrained_params = Ok(());
131137

132138
let hir_generics = node.generics().unwrap_or(NO_GENERICS);
133139
if let Node::Item(item) = node {
@@ -291,11 +297,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
291297
if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
292298
let self_ty = tcx.type_of(def_id).instantiate_identity();
293299
let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::instantiate_identity);
294-
cgp::setup_constraining_predicates(
295-
tcx,
296-
&mut predicates,
297-
trait_ref,
298-
&mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
300+
let mut input_parameters = cgp::parameters_for_impl(tcx, self_ty, trait_ref);
301+
cgp::setup_constraining_predicates(tcx, &mut predicates, trait_ref, &mut input_parameters);
302+
errored_due_to_unconstrained_params = errored_due_to_unconstrained_params.and(
303+
self_ty.error_reported().and(trait_ref.error_reported()).and_then(|()| {
304+
crate::impl_wf_check::enforce_impl_params_are_constrained(
305+
tcx,
306+
def_id,
307+
input_parameters,
308+
)
309+
}),
299310
);
300311
}
301312

@@ -338,6 +349,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
338349
parent: generics.parent,
339350
predicates: tcx.arena.alloc_from_iter(predicates),
340351
effects_min_tys: tcx.mk_type_list(&effects_min_tys),
352+
errored_due_to_unconstrained_params,
341353
}
342354
}
343355

@@ -489,6 +501,8 @@ pub(super) fn explicit_predicates_of<'tcx>(
489501
parent: predicates_and_bounds.parent,
490502
predicates: tcx.arena.alloc_slice(&predicates),
491503
effects_min_tys: predicates_and_bounds.effects_min_tys,
504+
errored_due_to_unconstrained_params: predicates_and_bounds
505+
.errored_due_to_unconstrained_params,
492506
}
493507
}
494508
} else {
@@ -541,6 +555,8 @@ pub(super) fn explicit_predicates_of<'tcx>(
541555
parent: parent_preds.parent,
542556
predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
543557
effects_min_tys: parent_preds.effects_min_tys,
558+
errored_due_to_unconstrained_params: parent_preds
559+
.errored_due_to_unconstrained_params,
544560
};
545561
}
546562
gather_explicit_predicates_of(tcx, def_id)
@@ -653,6 +669,7 @@ pub(super) fn implied_predicates_with_filter(
653669
parent: None,
654670
predicates: implied_bounds,
655671
effects_min_tys: ty::List::empty(),
672+
errored_due_to_unconstrained_params: Ok(()),
656673
}
657674
}
658675

@@ -688,7 +705,12 @@ pub(super) fn type_param_predicates(
688705
let icx = ItemCtxt::new(tcx, parent);
689706
icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_name)
690707
})
691-
.unwrap_or_default();
708+
.unwrap_or(GenericPredicates {
709+
parent: None,
710+
predicates: &[],
711+
effects_min_tys: ty::List::empty(),
712+
errored_due_to_unconstrained_params: Ok(()),
713+
});
692714
let mut extend = None;
693715

694716
let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
//! fixed, but for the moment it's easier to do these checks early.
1010
1111
use min_specialization::check_min_specialization;
12+
use rustc_data_structures::fx::FxHashSet;
1213
use rustc_errors::codes::*;
1314
use rustc_hir::def::DefKind;
1415
use rustc_hir::def_id::LocalDefId;
15-
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
16+
use rustc_middle::ty::{self, TyCtxt};
1617
use rustc_span::ErrorGuaranteed;
1718

1819
use crate::constrained_generic_params as cgp;

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
279279
}),
280280
),
281281
effects_min_tys: ty::List::empty(),
282+
errored_due_to_unconstrained_params: Ok(()),
282283
}
283284
}
284285

compiler/rustc_middle/src/ty/generics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::fx::FxHashMap;
33
use rustc_hir::def_id::DefId;
44
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
55
use rustc_span::symbol::{kw, Symbol};
6-
use rustc_span::Span;
6+
use rustc_span::{ErrorGuaranteed, Span};
77
use tracing::instrument;
88

99
use super::{Clause, InstantiatedPredicates, ParamConst, ParamTy, Ty, TyCtxt};
@@ -367,11 +367,12 @@ impl<'tcx> Generics {
367367
}
368368

369369
/// Bounds on generics.
370-
#[derive(Copy, Clone, Default, Debug, TyEncodable, TyDecodable, HashStable)]
370+
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
371371
pub struct GenericPredicates<'tcx> {
372372
pub parent: Option<DefId>,
373373
pub predicates: &'tcx [(Clause<'tcx>, Span)],
374374
pub effects_min_tys: &'tcx ty::List<Ty<'tcx>>,
375+
pub errored_due_to_unconstrained_params: Result<(), ErrorGuaranteed>,
375376
}
376377

377378
impl<'tcx> GenericPredicates<'tcx> {

compiler/rustc_smir/src/rustc_smir/context.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
161161
fn predicates_of(&self, def_id: stable_mir::DefId) -> stable_mir::ty::GenericPredicates {
162162
let mut tables = self.0.borrow_mut();
163163
let def_id = tables[def_id];
164-
let GenericPredicates { parent, predicates, effects_min_tys: _ } =
165-
tables.tcx.predicates_of(def_id);
164+
let GenericPredicates {
165+
parent,
166+
predicates,
167+
effects_min_tys: _,
168+
errored_due_to_unconstrained_params: _,
169+
} = tables.tcx.predicates_of(def_id);
166170
stable_mir::ty::GenericPredicates {
167171
parent: parent.map(|did| tables.trait_def(did)),
168172
predicates: predicates
@@ -183,8 +187,12 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
183187
) -> stable_mir::ty::GenericPredicates {
184188
let mut tables = self.0.borrow_mut();
185189
let def_id = tables[def_id];
186-
let GenericPredicates { parent, predicates, effects_min_tys: _ } =
187-
tables.tcx.explicit_predicates_of(def_id);
190+
let GenericPredicates {
191+
parent,
192+
predicates,
193+
effects_min_tys: _,
194+
errored_due_to_unconstrained_params: _,
195+
} = tables.tcx.explicit_predicates_of(def_id);
188196
stable_mir::ty::GenericPredicates {
189197
parent: parent.map(|did| tables.trait_def(did)),
190198
predicates: predicates

inc-fat/debuginfo_lto_alloc-3sqmtdvybgxcy/s-gyc0fcg7uo-1vs3i8e.lock

Whitespace-only changes.

src/librustdoc/clean/auto_trait.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ fn synthesize_auto_trait_impl<'tcx>(
103103
let mut generics = clean_ty_generics(
104104
cx,
105105
tcx.generics_of(item_def_id),
106-
ty::GenericPredicates::default(),
106+
ty::GenericPredicates {
107+
parent: None,
108+
predicates: &[],
109+
effects_min_tys: ty::List::empty(),
110+
errored_due_to_unconstrained_params: Ok(()),
111+
},
107112
);
108113
generics.where_predicates.clear();
109114

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,7 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
14111411
parent: None,
14121412
predicates,
14131413
effects_min_tys: ty::List::empty(),
1414+
errored_due_to_unconstrained_params: Ok(()),
14141415
},
14151416
);
14161417
simplify::move_bounds_to_generic_parameters(&mut generics);

tests/crashes/126646.rs renamed to tests/ui/associated-item/missing_method_in_impl_with_unconstrained_type_param.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
//@ known-bug: rust-lang/rust#126646
1+
//! This test used to ICE when trying to resolve the method call in the `test` function.
2+
23
mod foo {
34
pub trait Callable {
45
type Output;
56
fn call() -> Self::Output;
67
}
78

89
impl<'a, V: ?Sized> Callable for &'a () {
10+
//~^ ERROR: `V` is not constrained
911
type Output = ();
1012
}
1113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/missing_method_in_impl_with_unconstrained_type_param.rs:9:14
3+
|
4+
LL | impl<'a, V: ?Sized> Callable for &'a () {
5+
| ^ unconstrained type parameter
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0207`.

tests/ui/impl-unused-tps.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/impl-unused-tps.rs:47:8
3+
|
4+
LL | impl<T,U,V> Foo<T> for T
5+
| ^ unconstrained type parameter
6+
7+
error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
8+
--> $DIR/impl-unused-tps.rs:47:10
9+
|
10+
LL | impl<T,U,V> Foo<T> for T
11+
| ^ unconstrained type parameter
12+
13+
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
14+
--> $DIR/impl-unused-tps.rs:31:8
15+
|
16+
LL | impl<T,U> Bar for T {
17+
| ^ unconstrained type parameter
18+
19+
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
20+
--> $DIR/impl-unused-tps.rs:39:8
21+
|
22+
LL | impl<T,U> Bar for T
23+
| ^ unconstrained type parameter
24+
125
error[E0119]: conflicting implementations of trait `Foo<_>` for type `[isize; 0]`
226
--> $DIR/impl-unused-tps.rs:27:1
327
|
@@ -26,30 +50,6 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self
2650
LL | impl<T,U> Foo<T> for [isize;1] {
2751
| ^ unconstrained type parameter
2852

29-
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
30-
--> $DIR/impl-unused-tps.rs:31:8
31-
|
32-
LL | impl<T,U> Bar for T {
33-
| ^ unconstrained type parameter
34-
35-
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
36-
--> $DIR/impl-unused-tps.rs:39:8
37-
|
38-
LL | impl<T,U> Bar for T
39-
| ^ unconstrained type parameter
40-
41-
error[E0207]: the type parameter `U` is not constrained by the impl trait, self type, or predicates
42-
--> $DIR/impl-unused-tps.rs:47:8
43-
|
44-
LL | impl<T,U,V> Foo<T> for T
45-
| ^ unconstrained type parameter
46-
47-
error[E0207]: the type parameter `V` is not constrained by the impl trait, self type, or predicates
48-
--> $DIR/impl-unused-tps.rs:47:10
49-
|
50-
LL | impl<T,U,V> Foo<T> for T
51-
| ^ unconstrained type parameter
52-
5353
error: aborting due to 7 previous errors
5454

5555
Some errors have detailed explanations: E0119, E0207, E0275.

tests/ui/type-alias-impl-trait/ice-failed-to-resolve-instance-for-110696.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ note: this opaque type is in the signature
1111
LL | type DummyT<T> = impl F;
1212
| ^^^^^^
1313

14+
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
15+
--> $DIR/ice-failed-to-resolve-instance-for-110696.rs:41:6
16+
|
17+
LL | impl<T: MyFrom<Phantom2<DummyT<U>>>, U> MyIndex<DummyT<T>> for Scope<U> {
18+
| ^ unconstrained type parameter
19+
1420
error: item does not constrain `DummyT::{opaque#0}`, but has it in its signature
1521
--> $DIR/ice-failed-to-resolve-instance-for-110696.rs:44:8
1622
|
@@ -24,12 +30,6 @@ note: this opaque type is in the signature
2430
LL | type DummyT<T> = impl F;
2531
| ^^^^^^
2632

27-
error[E0207]: the type parameter `T` is not constrained by the impl trait, self type, or predicates
28-
--> $DIR/ice-failed-to-resolve-instance-for-110696.rs:41:6
29-
|
30-
LL | impl<T: MyFrom<Phantom2<DummyT<U>>>, U> MyIndex<DummyT<T>> for Scope<U> {
31-
| ^ unconstrained type parameter
32-
3333
error: aborting due to 3 previous errors
3434

3535
For more information about this error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)