Skip to content

Commit 63cb41f

Browse files
authored
Unrolled build for rust-lang#122192
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 auto-traits 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 c1a6199 + 8ea461d commit 63cb41f

31 files changed

+245
-263
lines changed

compiler/rustc_const_eval/src/check_consts/qualifs.rs

+27-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1268,7 +1268,7 @@ impl<'tcx> Ty<'tcx> {
12681268
///
12691269
/// Returning true means the type is known to be `Freeze`. Returning
12701270
/// `false` means nothing -- could be `Freeze`, might not be.
1271-
fn is_trivially_freeze(self) -> bool {
1271+
pub fn is_trivially_freeze(self) -> bool {
12721272
match self.kind() {
12731273
ty::Int(_)
12741274
| ty::Uint(_)

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
772772
);
773773
}
774774

775-
ty::Alias(ty::Opaque, _) => {
775+
ty::Alias(ty::Opaque, alias) => {
776776
if candidates.vec.iter().any(|c| matches!(c, ProjectionCandidate(_))) {
777777
// We do not generate an auto impl candidate for `impl Trait`s which already
778778
// reference our auto trait.
@@ -787,6 +787,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
787787
// We do not emit auto trait candidates for opaque types in coherence.
788788
// Doing so can result in weird dependency cycles.
789789
candidates.ambiguous = true;
790+
} else if self.infcx.can_define_opaque_ty(alias.def_id) {
791+
// We do not emit auto trait candidates for opaque types in their defining scope, as
792+
// we need to know the hidden type first, which we can't reliably know within the defining
793+
// scope.
794+
candidates.ambiguous = true;
790795
} else {
791796
candidates.vec.push(AutoImplCandidate)
792797
}

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -2386,13 +2386,17 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23862386
}
23872387

23882388
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => {
2389-
// We can resolve the `impl Trait` to its concrete type,
2390-
// which enforces a DAG between the functions requiring
2391-
// the auto trait bounds in question.
2392-
match self.tcx().type_of_opaque(def_id) {
2393-
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2394-
Err(_) => {
2395-
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2389+
if self.infcx.can_define_opaque_ty(def_id) {
2390+
unreachable!()
2391+
} else {
2392+
// We can resolve the `impl Trait` to its concrete type,
2393+
// which enforces a DAG between the functions requiring
2394+
// the auto trait bounds in question.
2395+
match self.tcx().type_of_opaque(def_id) {
2396+
Ok(ty) => t.rebind(vec![ty.instantiate(self.tcx(), args)]),
2397+
Err(_) => {
2398+
return Err(SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id));
2399+
}
23962400
}
23972401
}
23982402
}

tests/ui/const-generics/opaque_types.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ note: ...which requires const checking `main::{constant#0}`...
122122
|
123123
LL | foo::<42>();
124124
| ^^
125-
= note: ...which requires computing whether `Foo` is freeze...
126-
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Freeze`...
127125
= note: ...which again requires computing type of opaque `Foo::{opaque#0}`, completing the cycle
128126
note: cycle used when computing type of `Foo::{opaque#0}`
129127
--> $DIR/opaque_types.rs:3:12

tests/ui/consts/const-fn-cycle.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/// to end up revealing opaque types (the RPIT in `many`'s return type),
88
/// which can quickly lead to cycles.
99
10+
//@ check-pass
11+
1012
pub struct Parser<H>(H);
1113

1214
impl<H, T> Parser<H>
@@ -18,7 +20,6 @@ where
1820
}
1921

2022
pub const fn many<'s>(&'s self) -> Parser<impl for<'a> Fn(&'a str) -> Vec<T> + 's> {
21-
//~^ ERROR: cycle detected
2223
Parser::new(|_| unimplemented!())
2324
}
2425
}

tests/ui/consts/const-fn-cycle.stderr

-34
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
2-
--> $DIR/const-promoted-opaque.rs:29:25
2+
--> $DIR/const-promoted-opaque.rs:28:25
33
|
44
LL | let _: &'static _ = &FOO;
55
| ^^^^
@@ -9,7 +9,7 @@ LL | let _: &'static _ = &FOO;
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

1111
error[E0493]: destructor of `helper::Foo` cannot be evaluated at compile-time
12-
--> $DIR/const-promoted-opaque.rs:29:26
12+
--> $DIR/const-promoted-opaque.rs:28:26
1313
|
1414
LL | let _: &'static _ = &FOO;
1515
| ^^^ the destructor for this type cannot be evaluated in constants
@@ -18,13 +18,13 @@ LL | };
1818
| - value is dropped here
1919

2020
error[E0492]: constants cannot refer to interior mutable data
21-
--> $DIR/const-promoted-opaque.rs:34:19
21+
--> $DIR/const-promoted-opaque.rs:33:19
2222
|
2323
LL | const BAZ: &Foo = &FOO;
2424
| ^^^^ this borrow of an interior mutable value may end up in the final value
2525

2626
error[E0716]: temporary value dropped while borrowed
27-
--> $DIR/const-promoted-opaque.rs:38:26
27+
--> $DIR/const-promoted-opaque.rs:37:26
2828
|
2929
LL | let _: &'static _ = &FOO;
3030
| ---------- ^^^ creates a temporary value which is freed while still in use
@@ -34,38 +34,7 @@ LL |
3434
LL | }
3535
| - temporary value is freed at the end of this statement
3636

37-
error[E0391]: cycle detected when computing type of opaque `helper::Foo::{opaque#0}`
38-
--> $DIR/const-promoted-opaque.rs:14:20
39-
|
40-
LL | pub type Foo = impl Sized;
41-
| ^^^^^^^^^^
42-
|
43-
note: ...which requires borrow-checking `helper::FOO`...
44-
--> $DIR/const-promoted-opaque.rs:21:5
45-
|
46-
LL | pub const FOO: Foo = std::sync::atomic::AtomicU8::new(42);
47-
| ^^^^^^^^^^^^^^^^^^
48-
note: ...which requires promoting constants in MIR for `helper::FOO`...
49-
--> $DIR/const-promoted-opaque.rs:21:5
50-
|
51-
LL | pub const FOO: Foo = std::sync::atomic::AtomicU8::new(42);
52-
| ^^^^^^^^^^^^^^^^^^
53-
note: ...which requires const checking `helper::FOO`...
54-
--> $DIR/const-promoted-opaque.rs:21:5
55-
|
56-
LL | pub const FOO: Foo = std::sync::atomic::AtomicU8::new(42);
57-
| ^^^^^^^^^^^^^^^^^^
58-
= note: ...which requires computing whether `helper::Foo` is freeze...
59-
= note: ...which requires evaluating trait selection obligation `helper::Foo: core::marker::Freeze`...
60-
= note: ...which again requires computing type of opaque `helper::Foo::{opaque#0}`, completing the cycle
61-
note: cycle used when computing type of `helper::Foo::{opaque#0}`
62-
--> $DIR/const-promoted-opaque.rs:14:20
63-
|
64-
LL | pub type Foo = impl Sized;
65-
| ^^^^^^^^^^
66-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
67-
68-
error: aborting due to 5 previous errors
37+
error: aborting due to 4 previous errors
6938

70-
Some errors have detailed explanations: E0391, E0492, E0493, E0658, E0716.
71-
For more information about an error, try `rustc --explain E0391`.
39+
Some errors have detailed explanations: E0492, E0493, E0658, E0716.
40+
For more information about an error, try `rustc --explain E0492`.

tests/ui/consts/const-promoted-opaque.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
mod helper {
1414
pub type Foo = impl Sized;
15-
//[string,atomic]~^ ERROR cycle detected
1615

1716
#[cfg(string)]
1817
pub const FOO: Foo = String::new();
@@ -28,11 +27,11 @@ use helper::*;
2827
const BAR: () = {
2928
let _: &'static _ = &FOO;
3029
//[string,atomic]~^ ERROR: destructor of `helper::Foo` cannot be evaluated at compile-time
31-
//[string,atomic]~| ERROR: cannot borrow here
30+
//[atomic]~| ERROR: cannot borrow here
3231
};
3332

3433
const BAZ: &Foo = &FOO;
35-
//[string,atomic]~^ ERROR: constants cannot refer to interior mutable data
34+
//[atomic]~^ ERROR: constants cannot refer to interior mutable data
3635

3736
fn main() {
3837
let _: &'static _ = &FOO;
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
2-
--> $DIR/const-promoted-opaque.rs:29:25
3-
|
4-
LL | let _: &'static _ = &FOO;
5-
| ^^^^
6-
|
7-
= note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more information
8-
= help: add `#![feature(const_refs_to_cell)]` to the crate attributes to enable
9-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10-
111
error[E0493]: destructor of `helper::Foo` cannot be evaluated at compile-time
12-
--> $DIR/const-promoted-opaque.rs:29:26
2+
--> $DIR/const-promoted-opaque.rs:28:26
133
|
144
LL | let _: &'static _ = &FOO;
155
| ^^^ the destructor for this type cannot be evaluated in constants
166
...
177
LL | };
188
| - value is dropped here
199

20-
error[E0492]: constants cannot refer to interior mutable data
21-
--> $DIR/const-promoted-opaque.rs:34:19
22-
|
23-
LL | const BAZ: &Foo = &FOO;
24-
| ^^^^ this borrow of an interior mutable value may end up in the final value
25-
2610
error[E0716]: temporary value dropped while borrowed
27-
--> $DIR/const-promoted-opaque.rs:38:26
11+
--> $DIR/const-promoted-opaque.rs:37:26
2812
|
2913
LL | let _: &'static _ = &FOO;
3014
| ---------- ^^^ creates a temporary value which is freed while still in use
@@ -34,38 +18,7 @@ LL |
3418
LL | }
3519
| - temporary value is freed at the end of this statement
3620

37-
error[E0391]: cycle detected when computing type of opaque `helper::Foo::{opaque#0}`
38-
--> $DIR/const-promoted-opaque.rs:14:20
39-
|
40-
LL | pub type Foo = impl Sized;
41-
| ^^^^^^^^^^
42-
|
43-
note: ...which requires borrow-checking `helper::FOO`...
44-
--> $DIR/const-promoted-opaque.rs:18:5
45-
|
46-
LL | pub const FOO: Foo = String::new();
47-
| ^^^^^^^^^^^^^^^^^^
48-
note: ...which requires promoting constants in MIR for `helper::FOO`...
49-
--> $DIR/const-promoted-opaque.rs:18:5
50-
|
51-
LL | pub const FOO: Foo = String::new();
52-
| ^^^^^^^^^^^^^^^^^^
53-
note: ...which requires const checking `helper::FOO`...
54-
--> $DIR/const-promoted-opaque.rs:18:5
55-
|
56-
LL | pub const FOO: Foo = String::new();
57-
| ^^^^^^^^^^^^^^^^^^
58-
= note: ...which requires computing whether `helper::Foo` is freeze...
59-
= note: ...which requires evaluating trait selection obligation `helper::Foo: core::marker::Freeze`...
60-
= note: ...which again requires computing type of opaque `helper::Foo::{opaque#0}`, completing the cycle
61-
note: cycle used when computing type of `helper::Foo::{opaque#0}`
62-
--> $DIR/const-promoted-opaque.rs:14:20
63-
|
64-
LL | pub type Foo = impl Sized;
65-
| ^^^^^^^^^^
66-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
67-
68-
error: aborting due to 5 previous errors
21+
error: aborting due to 2 previous errors
6922

70-
Some errors have detailed explanations: E0391, E0492, E0493, E0658, E0716.
71-
For more information about an error, try `rustc --explain E0391`.
23+
Some errors have detailed explanations: E0493, E0716.
24+
For more information about an error, try `rustc --explain E0493`.
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`.
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`.
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() {}

0 commit comments

Comments
 (0)