Skip to content

Commit b335c2d

Browse files
Assemble Unpin candidates specially for generators in new solver
1 parent a41fc00 commit b335c2d

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::assembly::{self, structural_traits};
44
use super::{EvalCtxt, SolverMode};
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::LangItem;
6+
use rustc_hir::{LangItem, Movability};
77
use rustc_infer::traits::query::NoSolution;
88
use rustc_infer::traits::util::supertraits;
99
use rustc_middle::traits::solve::{CanonicalResponse, Certainty, Goal, QueryResult};
@@ -168,6 +168,23 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
168168

169169
ty::Infer(_) | ty::Bound(_, _) => bug!("unexpected type `{self_ty}`"),
170170

171+
// Generators have one special built-in candidate, `Unpin`, which
172+
// takes precedence over the structural auto trait candidate being
173+
// assembled.
174+
ty::Generator(_, _, movability)
175+
if Some(goal.predicate.def_id()) == ecx.tcx().lang_items().unpin_trait() =>
176+
{
177+
match movability {
178+
Movability::Static => {
179+
return Err(NoSolution);
180+
}
181+
Movability::Movable => {
182+
return ecx
183+
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
184+
}
185+
}
186+
}
187+
171188
// For rigid types, we only register a builtin auto implementation
172189
// if there is no implementation that could ever apply to the self
173190
// type.

tests/ui/generator/non-static-is-unpin.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// revisions: current next
2+
//[next] compile-flags: -Ztrait-solver=next
13
// run-pass
24

35
#![feature(generators, generator_trait)]

tests/ui/generator/static-not-unpin.stderr renamed to tests/ui/generator/static-not-unpin.current.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
error[E0277]: `[static generator@$DIR/static-not-unpin.rs:11:25: 11:34]` cannot be unpinned
2-
--> $DIR/static-not-unpin.rs:14:18
1+
error[E0277]: `[static generator@$DIR/static-not-unpin.rs:14:25: 14:34]` cannot be unpinned
2+
--> $DIR/static-not-unpin.rs:17:18
33
|
44
LL | assert_unpin(generator);
5-
| ------------ ^^^^^^^^^ the trait `Unpin` is not implemented for `[static generator@$DIR/static-not-unpin.rs:11:25: 11:34]`
5+
| ------------ ^^^^^^^^^ the trait `Unpin` is not implemented for `[static generator@$DIR/static-not-unpin.rs:14:25: 14:34]`
66
| |
77
| required by a bound introduced by this call
88
|
99
= note: consider using the `pin!` macro
1010
consider using `Box::pin` if you need to access the pinned value outside of the current scope
1111
note: required by a bound in `assert_unpin`
12-
--> $DIR/static-not-unpin.rs:7:20
12+
--> $DIR/static-not-unpin.rs:10:20
1313
|
1414
LL | fn assert_unpin<T: Unpin>(_: T) {
1515
| ^^^^^ required by this bound in `assert_unpin`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0277]: `[static generator@$DIR/static-not-unpin.rs:14:25: 14:34]` cannot be unpinned
2+
--> $DIR/static-not-unpin.rs:17:18
3+
|
4+
LL | assert_unpin(generator);
5+
| ------------ ^^^^^^^^^ the trait `Unpin` is not implemented for `[static generator@$DIR/static-not-unpin.rs:14:25: 14:34]`
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= note: consider using the `pin!` macro
10+
consider using `Box::pin` if you need to access the pinned value outside of the current scope
11+
note: required by a bound in `assert_unpin`
12+
--> $DIR/static-not-unpin.rs:10:20
13+
|
14+
LL | fn assert_unpin<T: Unpin>(_: T) {
15+
| ^^^^^ required by this bound in `assert_unpin`
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0277`.

tests/ui/generator/static-not-unpin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: current next
2+
//[next] compile-flags: -Ztrait-solver=next
3+
14
#![feature(generators)]
25

36
// normalize-stderr-test "std::pin::Unpin" -> "std::marker::Unpin"

0 commit comments

Comments
 (0)