8000 Don't allow `CoerceUnsized` into `dyn*` (except for trait upcasting) by compiler-errors · Pull Request #103386 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Don't allow CoerceUnsized into dyn* (except for trait upcasting) #103386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't CoerceUnsized dyn* to dyn*
  • Loading branch information
compiler-errors committed Nov 10, 2022
commit 6e6c49e7ced47224ecf1a9d14f63f9481fc24cce
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

match (source.kind(), target.kind()) {
// Trait+Kx+'a -> Trait+Ky+'b (upcasts).
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
(&ty::Dynamic(ref data_a, _, ty::Dyn), &ty::Dynamic(ref data_b, _, ty::Dyn)) => {
// Upcast coercions permit several things:
//
// 1. Dropping auto traits, e.g., `Foo + Send` to `Foo`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}));
}

_ => bug!(),
_ => bug!("source: {source}, target: {target}"),
};

Ok(ImplSourceBuiltinData { nested })
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/dyn-star/dont-unsize-coerce-dyn-star.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// check-pass

#![feature(dyn_star)]
#![allow(incomplete_features)]

trait AddOne {
fn add1(&mut self) -> usize;
}

impl AddOne for usize {
fn add1(&mut self) -> usize {
*self += 1;
*self
}
}

fn add_one(i: &mut (dyn* AddOne + '_)) -> usize {
i.add1()
}

fn main() {
let mut x = 42usize as dyn* AddOne;

println!("{}", add_one(&mut x));
println!("{}", add_one(&mut x));
}
0