From 0b5007e4b7c4ee9a3d0356e54f91ef3def094a0a Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 30 May 2020 19:42:22 +0200 Subject: [PATCH 1/3] merge coercion test folders --- .../ui/coerce/coerce-overloaded-autoderef.rs | 68 ---------------- .../coerce-expect-unsized.rs | 0 .../coerce-overloaded-autoderef-fail.rs | 32 ++++++++ ...> coerce-overloaded-autoderef-fail.stderr} | 8 +- .../coercion/coerce-overloaded-autoderef.rs | 78 ++++++++++++++----- .../coerce-reborrow-imm-ptr-arg.rs | 0 .../coerce-reborrow-imm-ptr-rcvr.rs | 0 .../coerce-reborrow-imm-vec-arg.rs | 0 .../coerce-reborrow-imm-vec-rcvr.rs | 0 .../coerce-reborrow-mut-ptr-arg.rs | 0 .../coerce-reborrow-mut-ptr-rcvr.rs | 0 .../coerce-reborrow-mut-vec-arg.rs | 0 .../coerce-reborrow-mut-vec-rcvr.rs | 0 src/test/ui/coercion/coerce-to-bang-cast.rs | 2 - .../ui/coercion/coerce-to-bang-cast.stderr | 4 +- .../coerce-unify-return.rs | 0 .../ui/{coerce => coercion}/coerce-unify.rs | 0 .../coerce-unsize-subtype.rs | 0 18 files changed, 95 insertions(+), 97 deletions(-) delete mode 100644 src/test/ui/coerce/coerce-overloaded-autoderef.rs rename src/test/ui/{coerce => coercion}/coerce-expect-unsized.rs (100%) create mode 100644 src/test/ui/coercion/coerce-overloaded-autoderef-fail.rs rename src/test/ui/coercion/{coerce-overloaded-autoderef.stderr => coerce-overloaded-autoderef-fail.stderr} (87%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-imm-ptr-arg.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-imm-ptr-rcvr.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-imm-vec-arg.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-imm-vec-rcvr.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-mut-ptr-arg.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-mut-ptr-rcvr.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-mut-vec-arg.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-reborrow-mut-vec-rcvr.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-unify-return.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-unify.rs (100%) rename src/test/ui/{coerce => coercion}/coerce-unsize-subtype.rs (100%) diff --git a/src/test/ui/coerce/coerce-overloaded-autoderef.rs b/src/test/ui/coerce/coerce-overloaded-autoderef.rs deleted file mode 100644 index d5484607c8b52..0000000000000 --- a/src/test/ui/coerce/coerce-overloaded-autoderef.rs +++ /dev/null @@ -1,68 +0,0 @@ -// run-pass -#![allow(unused_braces)] -#![allow(dead_code)] -// pretty-expanded FIXME #23616 - -use std::rc::Rc; - -// Examples from the "deref coercions" RFC, at rust-lang/rfcs#241. - -fn use_ref(_: &T) {} -fn use_mut(_: &mut T) {} - -fn use_rc(t: Rc) { - use_ref(&*t); // what you have to write today - use_ref(&t); // what you'd be able to write - use_ref(&&&&&&t); - use_ref(&mut &&&&&t); - use_ref(&&&mut &&&t); -} - -fn use_mut_box(mut t: &mut Box) { - use_mut(&mut *t); // what you have to write today - use_mut(t); // what you'd be able to write - use_mut(&mut &mut &mut t); - - use_ref(&*t); // what you have to write today - use_ref(t); // what you'd be able to write - use_ref(&&&&&&t); - use_ref(&mut &&&&&t); - use_ref(&&&mut &&&t); -} - -fn use_nested(t: &Box) { - use_ref(&**t); // what you have to write today - use_ref(t); // what you'd be able to write (note: recursive deref) - use_ref(&&&&&&t); - use_ref(&mut &&&&&t); - use_ref(&&&mut &&&t); -} - -fn use_slice(_: &[u8]) {} -fn use_slice_mut(_: &mut [u8]) {} - -fn use_vec(mut v: Vec) { - use_slice_mut(&mut v[..]); // what you have to write today - use_slice_mut(&mut v); // what you'd be able to write - use_slice_mut(&mut &mut &mut v); - - use_slice(&v[..]); // what you have to write today - use_slice(&v); // what you'd be able to write - use_slice(&&&&&&v); - use_slice(&mut &&&&&v); - use_slice(&&&mut &&&v); -} - -fn use_vec_ref(v: &Vec) { - use_slice(&v[..]); // what you have to write today - use_slice(v); // what you'd be able to write - use_slice(&&&&&&v); - use_slice(&mut &&&&&v); - use_slice(&&&mut &&&v); -} - -fn use_op_rhs(s: &mut String) { - *s += {&String::from(" ")}; -} - -pub fn main() {} diff --git a/src/test/ui/coerce/coerce-expect-unsized.rs b/src/test/ui/coercion/coerce-expect-unsized.rs similarity index 100% rename from src/test/ui/coerce/coerce-expect-unsized.rs rename to src/test/ui/coercion/coerce-expect-unsized.rs diff --git a/src/test/ui/coercion/coerce-overloaded-autoderef-fail.rs b/src/test/ui/coercion/coerce-overloaded-autoderef-fail.rs new file mode 100644 index 0000000000000..01d9c1e486a42 --- /dev/null +++ b/src/test/ui/coercion/coerce-overloaded-autoderef-fail.rs @@ -0,0 +1,32 @@ +fn borrow_mut(x: &mut T) -> &mut T { x } +fn borrow(x: &T) -> &T { x } + +fn borrow_mut2(_: &mut T, _: &mut T) {} +fn borrow2(_: &mut T, _: &T) {} + +fn double_mut_borrow(x: &mut Box) { + let y = borrow_mut(x); + let z = borrow_mut(x); + //~^ ERROR cannot borrow `*x` as mutable more than once at a time + drop((y, z)); +} + +fn double_imm_borrow(x: &mut Box) { + let y = borrow(x); + let z = borrow(x); + **x += 1; + //~^ ERROR cannot assign to `**x` because it is borrowed + drop((y, z)); +} + +fn double_mut_borrow2(x: &mut Box) { + borrow_mut2(x, x); + //~^ ERROR cannot borrow `*x` as mutable more than once at a time +} + +fn double_borrow2(x: &mut Box) { + borrow2(x, x); + //~^ ERROR cannot borrow `*x` as mutable because it is also borrowed as immutable +} + +pub fn main() {} diff --git a/src/test/ui/coercion/coerce-overloaded-autoderef.stderr b/src/test/ui/coercion/coerce-overloaded-autoderef-fail.stderr similarity index 87% rename from src/test/ui/coercion/coerce-overloaded-autoderef.stderr rename to src/test/ui/coercion/coerce-overloaded-autoderef-fail.stderr index 7cdfcb5f4fc63..d067c3b3a1805 100644 --- a/src/test/ui/coercion/coerce-overloaded-autoderef.stderr +++ b/src/test/ui/coercion/coerce-overloaded-autoderef-fail.stderr @@ -1,5 +1,5 @@ error[E0499]: cannot borrow `*x` as mutable more than once at a time - --> $DIR/coerce-overloaded-autoderef.rs:9:24 + --> $DIR/coerce-overloaded-autoderef-fail.rs:9:24 | LL | let y = borrow_mut(x); | - first mutable borrow occurs here @@ -10,7 +10,7 @@ LL | drop((y, z)); | - first borrow later used here error[E0506]: cannot assign to `**x` because it is borrowed - --> $DIR/coerce-overloaded-autoderef.rs:17:5 + --> $DIR/coerce-overloaded-autoderef-fail.rs:17:5 | LL | let y = borrow(x); | - borrow of `**x` occurs here @@ -22,7 +22,7 @@ LL | drop((y, z)); | - borrow later used here error[E0499]: cannot borrow `*x` as mutable more than once at a time - --> $DIR/coerce-overloaded-autoderef.rs:23:20 + --> $DIR/coerce-overloaded-autoderef-fail.rs:23:20 | LL | borrow_mut2(x, x); | ----------- - ^ second mutable borrow occurs here @@ -31,7 +31,7 @@ LL | borrow_mut2(x, x); | first borrow later used by call error[E0502]: cannot borrow `*x` as mutable because it is also borrowed as immutable - --> $DIR/coerce-overloaded-autoderef.rs:28:5 + --> $DIR/coerce-overloaded-autoderef-fail.rs:28:5 | LL | borrow2(x, x); | -------^^^^-^ diff --git a/src/test/ui/coercion/coerce-overloaded-autoderef.rs b/src/test/ui/coercion/coerce-overloaded-autoderef.rs index 01d9c1e486a42..d5484607c8b52 100644 --- a/src/test/ui/coercion/coerce-overloaded-autoderef.rs +++ b/src/test/ui/coercion/coerce-overloaded-autoderef.rs @@ -1,32 +1,68 @@ -fn borrow_mut(x: &mut T) -> &mut T { x } -fn borrow(x: &T) -> &T { x } +// run-pass +#![allow(unused_braces)] +#![allow(dead_code)] +// pretty-expanded FIXME #23616 -fn borrow_mut2(_: &mut T, _: &mut T) {} -fn borrow2(_: &mut T, _: &T) {} +use std::rc::Rc; -fn double_mut_borrow(x: &mut Box) { - let y = borrow_mut(x); - let z = borrow_mut(x); - //~^ ERROR cannot borrow `*x` as mutable more than once at a time - drop((y, z)); +// Examples from the "deref coercions" RFC, at rust-lang/rfcs#241. + +fn use_ref(_: &T) {} +fn use_mut(_: &mut T) {} + +fn use_rc(t: Rc) { + use_ref(&*t); // what you have to write today + use_ref(&t); // what you'd be able to write + use_ref(&&&&&&t); + use_ref(&mut &&&&&t); + use_ref(&&&mut &&&t); +} + +fn use_mut_box(mut t: &mut Box) { + use_mut(&mut *t); // what you have to write today + use_mut(t); // what you'd be able to write + use_mut(&mut &mut &mut t); + + use_ref(&*t); // what you have to write today + use_ref(t); // what you'd be able to write + use_ref(&&&&&&t); + use_ref(&mut &&&&&t); + use_ref(&&&mut &&&t); } -fn double_imm_borrow(x: &mut Box) { - let y = borrow(x); - let z = borrow(x); - **x += 1; - //~^ ERROR cannot assign to `**x` because it is borrowed - drop((y, z)); +fn use_nested(t: &Box) { + use_ref(&**t); // what you have to write today + use_ref(t); // what you'd be able to write (note: recursive deref) + use_ref(&&&&&&t); + use_ref(&mut &&&&&t); + use_ref(&&&mut &&&t); +} + +fn use_slice(_: &[u8]) {} +fn use_slice_mut(_: &mut [u8]) {} + +fn use_vec(mut v: Vec) { + use_slice_mut(&mut v[..]); // what you have to write today + use_slice_mut(&mut v); // what you'd be able to write + use_slice_mut(&mut &mut &mut v); + + use_slice(&v[..]); // what you have to write today + use_slice(&v); // what you'd be able to write + use_slice(&&&&&&v); + use_slice(&mut &&&&&v); + use_slice(&&&mut &&&v); } -fn double_mut_borrow2(x: &mut Box) { - borrow_mut2(x, x); - //~^ ERROR cannot borrow `*x` as mutable more than once at a time +fn use_vec_ref(v: &Vec) { + use_slice(&v[..]); // what you have to write today + use_slice(v); // what you'd be able to write + use_slice(&&&&&&v); + use_slice(&mut &&&&&v); + use_slice(&&&mut &&&v); } -fn double_borrow2(x: &mut Box) { - borrow2(x, x); - //~^ ERROR cannot borrow `*x` as mutable because it is also borrowed as immutable +fn use_op_rhs(s: &mut String) { + *s += {&String::from(" ")}; } pub fn main() {} diff --git a/src/test/ui/coerce/coerce-reborrow-imm-ptr-arg.rs b/src/test/ui/coercion/coerce-reborrow-imm-ptr-arg.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-imm-ptr-arg.rs rename to src/test/ui/coercion/coerce-reborrow-imm-ptr-arg.rs diff --git a/src/test/ui/coerce/coerce-reborrow-imm-ptr-rcvr.rs b/src/test/ui/coercion/coerce-reborrow-imm-ptr-rcvr.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-imm-ptr-rcvr.rs rename to src/test/ui/coercion/coerce-reborrow-imm-ptr-rcvr.rs diff --git a/src/test/ui/coerce/coerce-reborrow-imm-vec-arg.rs b/src/test/ui/coercion/coerce-reborrow-imm-vec-arg.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-imm-vec-arg.rs rename to src/test/ui/coercion/coerce-reborrow-imm-vec-arg.rs diff --git a/src/test/ui/coerce/coerce-reborrow-imm-vec-rcvr.rs b/src/test/ui/coercion/coerce-reborrow-imm-vec-rcvr.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-imm-vec-rcvr.rs rename to src/test/ui/coercion/coerce-reborrow-imm-vec-rcvr.rs diff --git a/src/test/ui/coerce/coerce-reborrow-mut-ptr-arg.rs b/src/test/ui/coercion/coerce-reborrow-mut-ptr-arg.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-mut-ptr-arg.rs rename to src/test/ui/coercion/coerce-reborrow-mut-ptr-arg.rs diff --git a/src/test/ui/coerce/coerce-reborrow-mut-ptr-rcvr.rs b/src/test/ui/coercion/coerce-reborrow-mut-ptr-rcvr.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-mut-ptr-rcvr.rs rename to src/test/ui/coercion/coerce-reborrow-mut-ptr-rcvr.rs diff --git a/src/test/ui/coerce/coerce-reborrow-mut-vec-arg.rs b/src/test/ui/coercion/coerce-reborrow-mut-vec-arg.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-mut-vec-arg.rs rename to src/test/ui/coercion/coerce-reborrow-mut-vec-arg.rs diff --git a/src/test/ui/coerce/coerce-reborrow-mut-vec-rcvr.rs b/src/test/ui/coercion/coerce-reborrow-mut-vec-rcvr.rs similarity index 100% rename from src/test/ui/coerce/coerce-reborrow-mut-vec-rcvr.rs rename to src/test/ui/coercion/coerce-reborrow-mut-vec-rcvr.rs diff --git a/src/test/ui/coercion/coerce-to-bang-cast.rs b/src/test/ui/coercion/coerce-to-bang-cast.rs index 8ef1948084654..85598a42eccd9 100644 --- a/src/test/ui/coercion/coerce-to-bang-cast.rs +++ b/src/test/ui/coercion/coerce-to-bang-cast.rs @@ -1,7 +1,5 @@ #![feature(never_type)] -fn foo(x: usize, y: !, z: usize) { } - fn cast_a() { let y = {return; 22} as !; //~^ ERROR non-primitive cast diff --git a/src/test/ui/coercion/coerce-to-bang-cast.stderr b/src/test/ui/coercion/coerce-to-bang-cast.stderr index d3adbd5158dbb..50e009aa25bb1 100644 --- a/src/test/ui/coercion/coerce-to-bang-cast.stderr +++ b/src/test/ui/coercion/coerce-to-bang-cast.stderr @@ -1,11 +1,11 @@ error[E0605]: non-primitive cast: `i32` as `!` - --> $DIR/coerce-to-bang-cast.rs:6:13 + --> $DIR/coerce-to-bang-cast.rs:4:13 | LL | let y = {return; 22} as !; | ^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object error[E0605]: non-primitive cast: `i32` as `!` - --> $DIR/coerce-to-bang-cast.rs:11:13 + --> $DIR/coerce-to-bang-cast.rs:9:13 | LL | let y = 22 as !; | ^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object diff --git a/src/test/ui/coerce/coerce-unify-return.rs b/src/test/ui/coercion/coerce-unify-return.rs similarity index 100% rename from src/test/ui/coerce/coerce-unify-return.rs rename to src/test/ui/coercion/coerce-unify-return.rs diff --git a/src/test/ui/coerce/coerce-unify.rs b/src/test/ui/coercion/coerce-unify.rs similarity index 100% rename from src/test/ui/coerce/coerce-unify.rs rename to src/test/ui/coercion/coerce-unify.rs diff --git a/src/test/ui/coerce/coerce-unsize-subtype.rs b/src/test/ui/coercion/coerce-unsize-subtype.rs similarity index 100% rename from src/test/ui/coerce/coerce-unsize-subtype.rs rename to src/test/ui/coercion/coerce-unsize-subtype.rs From 9b47586a47888f24f8f5565731db44480cce7e28 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 30 May 2020 19:43:04 +0200 Subject: [PATCH 2/3] update coercion docs --- src/librustc_typeck/check/coercion.rs | 49 ++++++++++----------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/src/librustc_typeck/check/coercion.rs b/src/librustc_typeck/check/coercion.rs index dd3f292fb5684..aac4466b6abab 100644 --- a/src/librustc_typeck/check/coercion.rs +++ b/src/librustc_typeck/check/coercion.rs @@ -10,45 +10,30 @@ //! //! Note that if we are expecting a reference, we will *reborrow* //! even if the argument provided was already a reference. This is -//! useful for freezing mut/const things (that is, when the expected is &T -//! but you have &const T or &mut T) and also for avoiding the linearity +//! useful for freezing mut things (that is, when the expected type is &T +//! but you have &mut T) and also for avoiding the linearity //! of mut things (when the expected is &mut T and you have &mut T). See -//! the various `src/test/ui/coerce-reborrow-*.rs` tests for +//! the various `src/test/ui/coerce/*.rs` tests for //! examples of where this is useful. //! //! ## Subtle note //! -//! When deciding what type coercions to consider, we do not attempt to -//! resolve any type variables we may encounter. This is because `b` -//! represents the expected type "as the user wrote it", meaning that if -//! the user defined a generic function like +//! When infering the generic arguments of functions, the argument +//! order is relevant, which can lead to the following edge case: //! -//! fn foo(a: A, b: A) { ... } +//! ```rust +//! fn foo(a: T, b: T) { +//! // ... +//! } //! -//! and then we wrote `foo(&1, @2)`, we will not auto-borrow -//! either argument. In older code we went to some lengths to -//! resolve the `b` variable, which could mean that we'd -//! auto-borrow later arguments but not earlier ones, which -//! seems very confusing. +//! foo(&7i32, &mut 7i32); +//! // This compiles, as we first infer `T` to be `&i32`, +//! // and then coerce `&mut 7i32` to `&7i32`. //! -//! ## Subtler note -//! -//! However, right now, if the user manually specifies the -//! values for the type variables, as so: -//! -//! foo::<&int>(@1, @2) -//! -//! then we *will* auto-borrow, because we can't distinguish this from a -//! function that declared `&int`. This is inconsistent but it's easiest -//! at the moment. The right thing to do, I think, is to consider the -//! *unsubstituted* type when deciding whether to auto-borrow, but the -//! *substituted* type when considering the bounds and so forth. But most -//! of our methods don't give access to the unsubstituted type, and -//! rightly so because they'd be error-prone. So maybe the thing to do is -//! to actually determine the kind of coercions that should occur -//! separately and pass them in. Or maybe it's ok as is. Anyway, it's -//! sort of a minor point so I've opted to leave it for later -- after all, -//! we may want to adjust precisely when coercions occur. +//! foo(&mut 7i32, &7i32); +//! // This does not compile, as we first infer `T` to be `&mut i32` +//! // and are then unable to coerce `&7i32` to `&mut i32`. +//! ``` use crate::astconv::AstConv; use crate::check::{FnCtxt, Needs}; @@ -96,6 +81,8 @@ impl<'a, 'tcx> Deref for Coerce<'a, 'tcx> { type CoerceResult<'tcx> = InferResult<'tcx, (Vec>, Ty<'tcx>)>; +/// Coercing a mutable reference to an immutable works, while +/// coercing `&T` to `&mut T` should be forbidden. fn coerce_mutbls<'tcx>( from_mutbl: hir::Mutability, to_mutbl: hir::Mutability, From 06a237fe2c3b83d26bd4b5aa74dc9aa47a07d9a9 Mon Sep 17 00:00:00 2001 From: Bastian Kauschke Date: Sat, 30 May 2020 20:54:14 +0200 Subject: [PATCH 3/3] coerce reborrow multi arg test --- .../ui/coercion/coerce-reborrow-multi-arg-fail.rs | 6 ++++++ .../coercion/coerce-reborrow-multi-arg-fail.stderr | 12 ++++++++++++ src/test/ui/coercion/coerce-reborrow-multi-arg.rs | 9 +++++++++ 3 files changed, 27 insertions(+) create mode 100644 src/test/ui/coercion/coerce-reborrow-multi-arg-fail.rs create mode 100644 src/test/ui/coercion/coerce-reborrow-multi-arg-fail.stderr create mode 100644 src/test/ui/coercion/coerce-reborrow-multi-arg.rs diff --git a/src/test/ui/coercion/coerce-reborrow-multi-arg-fail.rs b/src/test/ui/coercion/coerce-reborrow-multi-arg-fail.rs new file mode 100644 index 0000000000000..48be2d3146b81 --- /dev/null +++ b/src/test/ui/coercion/coerce-reborrow-multi-arg-fail.rs @@ -0,0 +1,6 @@ +fn test(_a: T, _b: T) {} + +fn main() { + test(&mut 7, &7); + //~^ mismatched types +} diff --git a/src/test/ui/coercion/coerce-reborrow-multi-arg-fail.stderr b/src/test/ui/coercion/coerce-reborrow-multi-arg-fail.stderr new file mode 100644 index 0000000000000..59b0ec496f16f --- /dev/null +++ b/src/test/ui/coercion/coerce-reborrow-multi-arg-fail.stderr @@ -0,0 +1,12 @@ +error[E0308]: mismatched types + --> $DIR/coerce-reborrow-multi-arg-fail.rs:4:18 + | +LL | test(&mut 7, &7); + | ^^ types differ in mutability + | + = note: expected mutable reference `&mut {integer}` + found reference `&{integer}` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/coercion/coerce-reborrow-multi-arg.rs b/src/test/ui/coercion/coerce-reborrow-multi-arg.rs new file mode 100644 index 0000000000000..93cd0bb3e27f6 --- /dev/null +++ b/src/test/ui/coercion/coerce-reborrow-multi-arg.rs @@ -0,0 +1,9 @@ +// build-pass +fn test(_a: T, _b: T) {} + +fn main() { + test(&7, &7); + test(&7, &mut 7); + test::<&i32>(&mut 7, &7); + test::<&i32>(&mut 7, &mut 7); +}