Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c43eb8e

Browse files
authored
Rollup merge of rust-lang#122192 - oli-obk:type_of_opaque_for_const_checks, r=lcnr
Do not try to reveal hidden types when trying to prove Freeze in the defining scope fixes rust-lang#99793 this avoids the cycle error by just causing a selection error, which is not fatal. We pessimistically assume that freeze does not hold, which is always a safe assumption.
2 parents a330e49 + ad6e85b commit c43eb8e

File tree

16 files changed

+211
-68
lines changed

16 files changed

+211
-68
lines changed

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,33 @@ impl Qualif for HasMutInterior {
100100
}
101101

102102
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
103-
!ty.is_freeze(cx.tcx, cx.param_env)
103+
// Avoid selecting for simple cases, such as builtin types.
104+
if ty.is_trivially_freeze() {
105+
return false;
106+
}
107+
108+
// We do not use `ty.is_freeze` here, because that requires revealing opaque types, which
109+
// requires borrowck, which in turn will invoke mir_const_qualifs again, causing a cycle error.
110+
// Instead we invoke an obligation context manually, and provide the opaque type inference settings
111+
// that allow the trait solver to just error out instead of cycling.
112+
let freeze_def_id = cx.tcx.require_lang_item(LangItem::Freeze, Some(cx.body.span));
113+
114+
let obligation = Obligation::new(
115+
cx.tcx,
116+
ObligationCause::dummy_with_span(cx.body.span),
117+
cx.param_env,
118+
ty::TraitRef::new(cx.tcx, freeze_def_id, [ty::GenericArg::from(ty)]),
119+
);
120+
121+
let infcx = cx
122+
.tcx
123+
.infer_ctxt()
124+
.with_opaque_type_inference(cx.body.source.def_id().expect_local())
125+
.build();
126+
let ocx = ObligationCtxt::new(&infcx);
127+
ocx.register_obligation(obligation);
128+
let errors = ocx.select_all_or_error();
129+
!errors.is_empty()
104130
}
105131

106132
fn in_adt_inherently<'tcx>(

compiler/rustc_middle/src/ty/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ impl<'tcx> Ty<'tcx> {
13021302
///
13031303
/// Returning true means the type is known to be `Freeze`. Returning
13041304
/// `false` means nothing -- could be `Freeze`, might not be.
1305-
fn is_trivially_freeze(self) -> bool {
1305+
pub fn is_trivially_freeze(self) -> bool {
13061306
match self.kind() {
13071307
ty::Int(_)
13081308
| ty::Uint(_)

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
777777
);
778778
}
779779

780-
ty::Alias(ty::Opaque, _) => {
780+
ty::Alias(ty::Opaque, alias) => {
781781
if candidates.vec.iter().any(|c| matches!(c, ProjectionCandidate(_))) {
782782
// We do not generate an auto impl candidate for `impl Trait`s which already
783783
// reference our auto trait.
@@ -792,6 +792,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
792792
// We do not emit auto trait candidates for opaque types in coherence.
793793
// Doing so can result in weird dependency cycles.
794794
candidates.ambiguous = true;
795+
} else if self.infcx.can_define_opaque_ty(alias.def_id) {
796+
// We do not emit auto trait candidates for opaque types in their defining scope, as
797+
// we need to know the hidden type first, which we can't reliably know within the defining
798+
// scope.
799+
candidates.ambiguous = true;
795800
} else {
796801
candidates.vec.push(AutoImplCandidate)
797802
}

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,13 +2417,18 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
24172417
}
24182418

24192419
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
2420-
// We can resolve the `impl Trait` to its concrete type,
2421-
// which enforces a DAG between the functions requiring
2422-
// the auto trait bounds in question.
2423-
match self.tcx().type_of_opaque(def_id) {
2424-
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2425-
Err(_) => {
2426-
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2420+
if self.infcx.can_define_opaque_ty(def_id) {
2421+
// We cannot possibly resolve this opaque type, because we are currently computing its hidden type.
2422+
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2423+
} else {
2424+
// We can resolve the `impl Trait` to its concrete type,
2425+
// which enforces a DAG between the functions requiring
2426+
// the auto trait bounds in question.
2427+
match self.tcx().type_of_opaque(def_id) {
2428+
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2429+
Err(_) => {
2430+
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2431+
}
24272432
}
24282433
}
24292434
}

tests/ui/const-generics/opaque_types.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ note: ...which requires const checking `main::{constant#0}`...
109109
|
110110
LL | foo::<42>();
111111
| ^^
112-
= note: ...which requires computing whether `Foo` is freeze...
113-
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Freeze`...
114112
= note: ...which again requires computing type of opaque `Foo::{opaque#0}`, completing the cycle
115113
note: cycle used when computing type of `Foo::{opaque#0}`
116114
--> $DIR/opaque_types.rs:3:12
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/auto-trait-selection-freeze.rs:19:16
3+
|
4+
LL | if false { is_trait(foo()) } else { Default::default() }
5+
| ^^^^^^^^ ----- type must be known at this point
6+
| |
7+
| cannot infer type of the type parameter `T` declared on the function `is_trait`
8+
|
9+
= note: cannot satisfy `_: Trait<_>`
10+
note: required by a bound in `is_trait`
11+
--> $DIR/auto-trait-selection-freeze.rs:11:16
12+
|
13+
LL | fn is_trait<T: Trait<U>, U: Default>(_: T) -> U {
14+
| ^^^^^^^^ required by this bound in `is_trait`
15+
help: consider specifying the generic arguments
16+
|
17+
LL | if false { is_trait::<T, U>(foo()) } else { Default::default() }
18+
| ++++++++
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0283`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/auto-trait-selection-freeze.rs:19:16
3+
|
4+
LL | if false { is_trait(foo()) } else { Default::default() }
5+
| ^^^^^^^^ cannot infer type of the type parameter `U` declared on the function `is_trait`
6+
|
7+
note: multiple `impl`s satisfying `impl Sized: Trait<_>` found
8+
--> $DIR/auto-trait-selection-freeze.rs:16:1
9+
|
10+
LL | impl<T: Freeze> Trait<u32> for T {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL | impl<T> Trait<i32> for T {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^
14+
note: required by a bound in `is_trait`
15+
--> $DIR/auto-trait-selection-freeze.rs:11:16
16+
|
17+
LL | fn is_trait<T: Trait<U>, U: Default>(_: T) -> U {
18+
| ^^^^^^^^ required by this bound in `is_trait`
19+
help: consider specifying the generic arguments
20+
|
21+
LL | if false { is_trait::<_, U>(foo()) } else { Default::default() }
22+
| ++++++++
23+
24+
error: aborting due to 1 previous error
25+
26+
For more information about this error, try `rustc --explain E0283`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! This test shows how we fail selection in a way that can influence
2+
//! selection in a code path that succeeds.
3+
4+
//@ revisions: next old
5+
//@[next] compile-flags: -Znext-solver
6+
7+
#![feature(freeze)]
8+
9+
use std::marker::Freeze;
10+
11+
fn is_trait<T: Trait<U>, U: Default>(_: T) -> U {
12+
Default::default()
13+
}
14+
15+
trait Trait<T> {}
16+
impl<T: Freeze> Trait<u32> for T {}
17+
impl<T> Trait<i32> for T {}
18+
fn foo() -> impl Sized {
19+
if false { is_trait(foo()) } else { Default::default() }
20+
//~^ ERROR: type annotations needed
21+
}
22+
23+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/auto-trait-selection.rs:15:16
3+
|
4+
LL | if false { is_trait(foo()) } else { Default::default() }
5+
| ^^^^^^^^ ----- type must be known at this point
6+
| |
7+
| cannot infer type of the type parameter `T` declared on the function `is_trait`
8+
|
9+
= note: cannot satisfy `_: Trait<_>`
10+
note: required by a bound in `is_trait`
11+
--> $DIR/auto-trait-selection.rs:7:16
12+
|
13+
LL | fn is_trait<T: Trait<U>, U: Default>(_: T) -> U {
14+
| ^^^^^^^^ required by this bound in `is_trait`
15+
help: consider specifying the generic arguments
16+
|
17+
LL | if false { is_trait::<T, U>(foo()) } else { Default::default() }
18+
| ++++++++
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0283`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0283]: type annotations needed
2+
--> $DIR/auto-trait-selection.rs:15:16
3+
|
4+
LL | if false { is_trait(foo()) } else { Default::default() }
5+
| ^^^^^^^^ cannot infer type of the type parameter `U` declared on the function `is_trait`
6+
|
7+
note: multiple `impl`s satisfying `impl Sized: Trait<_>` found
8+
--> $DIR/auto-trait-selection.rs:12:1
9+
|
10+
LL | impl<T: Send> Trait<u32> for T {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL | impl<T> Trait<i32> for T {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^
14+
note: required by a bound in `is_trait`
15+
--> $DIR/auto-trait-selection.rs:7:16
16+
|
17+
LL | fn is_trait<T: Trait<U>, U: Default>(_: T) -> U {
18+
| ^^^^^^^^ required by this bound in `is_trait`
19+
help: consider specifying the generic arguments
20+
|
21+
LL | if false { is_trait::<_, U>(foo()) } else { Default::default() }
22+
| ++++++++
23+
24+
error: aborting due to 1 previous error
25+
26+
For more information about this error, try `rustc --explain E0283`.

0 commit comments

Comments
 (0)