Skip to content

Commit

Permalink
Don't type error if we fail to coerce Pin because it doesnt contain a…
Browse files Browse the repository at this point in the history
… ref
  • Loading branch information
compiler-errors committed Nov 23, 2024
1 parent dd920fa commit 9455373
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
11 changes: 8 additions & 3 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
}

// Examine the supertype and consider auto-borrowing.
// Examine the supertype and consider type-specific coercions, such
// as auto-borrowing, coercing pointer mutability, a `dyn*` coercion,
// or pin-ergonomics.
match *b.kind() {
ty::RawPtr(_, b_mutbl) => {
return self.coerce_unsafe_ptr(a, b, b_mutbl);
Expand All @@ -230,7 +232,10 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
if self.tcx.features().pin_ergonomics()
&& self.tcx.is_lang_item(pin.did(), hir::LangItem::Pin) =>
{
return self.coerce_pin(a, b);
let pin_coerce = self.commit_if_ok(|_| self.coerce_pin_ref(a, b));
if pin_coerce.is_ok() {
return pin_coerce;
}
}
_ => {}
}
Expand Down Expand Up @@ -797,7 +802,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
/// - `Pin<Box<T>>` as `Pin<&T>`
/// - `Pin<Box<T>>` as `Pin<&mut T>`
#[instrument(skip(self), level = "trace")]
fn coerce_pin(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
fn coerce_pin_ref(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
// We need to make sure the two types are compatible for coercion.
// Then we will build a ReborrowPin adjustment and return that as an InferOk.

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/async-await/pin-ergonomics/coerce-non-pointer-pin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ check-pass

#![feature(pin_ergonomics)]
//~^ WARN the feature `pin_ergonomics` is incomplete

use std::pin::Pin;

fn main() {
let _: Pin<Box<()>> = Box::pin(());
}
11 changes: 11 additions & 0 deletions tests/ui/async-await/pin-ergonomics/coerce-non-pointer-pin.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: the feature `pin_ergonomics` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/coerce-non-pointer-pin.rs:3:12
|
LL | #![feature(pin_ergonomics)]
| ^^^^^^^^^^^^^^
|
= note: see issue #130494 <https://github.com/rust-lang/rust/issues/130494> for more information
= note: `#[warn(incomplete_features)]` on by default

warning: 1 warning emitted

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/pin-reborrow-const-as-mut.rs:14:9
--> $DIR/reborrow-const-as-mut.rs:14:9
|
LL | foo(x);
| --- ^ types differ in mutability
Expand All @@ -9,7 +9,7 @@ LL | foo(x);
= note: expected struct `Pin<&mut Foo>`
found struct `Pin<&Foo>`
note: function defined here
--> $DIR/pin-reborrow-const-as-mut.rs:10:4
--> $DIR/reborrow-const-as-mut.rs:10:4
|
LL | fn foo(_: Pin<&mut Foo>) {
| ^^^ ----------------
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/pin-ergonomics/reborrow-once.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0499]: cannot borrow `*x.__pointer` as mutable more than once at a time
--> $DIR/pin-reborrow-once.rs:12:14
--> $DIR/reborrow-once.rs:12:14
|
LL | twice(x, x);
| ----- - ^ second mutable borrow occurs here
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/pin-ergonomics/sugar-no-const.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: expected one of `!`, `(`, `::`, `;`, `<`, or `=`, found `i32`
--> $DIR/pin-sugar-no-const.rs:7:18
--> $DIR/sugar-no-const.rs:7:18
|
LL | let _x: &pin i32 = todo!();
| - ^^^ expected one of `!`, `(`, `::`, `;`, `<`, or `=`
Expand Down

0 comments on commit 9455373

Please sign in to comment.