Skip to content
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

fix: Disable invalid cast diagnostics for casts like &T as &dyn Trait #18054

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 22 additions & 1 deletion crates/hir-ty/src/infer/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,28 @@ impl CastCheck {
}
_ => return Err(CastError::NonScalar),
},
_ => return Err(CastError::NonScalar),
_ => {
// We fail to coerce to dyn Trait sometimes due to lack of some functionalities
// under current chalk trait solver. (See #11847 and #18047)
// So, emit no diagnostic for casts like `&T as &dyn Trait` for now,
// to prevent false positive diagnositcs.
// Direct cast without ref like `T as dyn Trait` is prohibited by
// `InferenceDiagnostic::CastToUnsized`, so allowing this diagnostics bypass
// only to `&T as &dyn Trait` is sufficient
if let (TyKind::Ref(m_expr, _, _), TyKind::Ref(m_cast, _, t_cast)) =
(self.expr_ty.kind(Interner), self.cast_ty.kind(Interner))
{
// Refuse casting `&T` into `&mut U`
if m_expr > m_cast {
return Err(CastError::IllegalCast);
}
if matches!(table.resolve_ty_shallow(t_cast).kind(Interner), TyKind::Dyn(_))
{
return Ok(());
}
}
return Err(CastError::NonScalar);
}
};

// rustc checks whether the `expr_ty` is foreign adt with `non_exhaustive` sym
Expand Down
30 changes: 21 additions & 9 deletions crates/ide-diagnostics/src/handlers/invalid_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub fn main() {
fn dyn_tail_need_normalization() {
check_diagnostics(
r#"
//- minicore: dispatch_from_dyn
//- minicore: coerce_unsized, dispatch_from_dyn
trait Trait {
type Associated;
}
Expand Down Expand Up @@ -370,7 +370,7 @@ fn main() {
fn fat_ptr_cast() {
check_diagnostics_with_disabled(
r#"
//- minicore: sized
//- minicore: coerce_unsized, dispatch_from_dyn
trait Foo {
fn foo(&self) {} //~ WARN method `foo` is never used
}
Expand Down Expand Up @@ -399,12 +399,12 @@ fn main() {
let d = to_raw(a) as usize;
}
"#,
&["E0308"],
&["E0308", "unused_variables"],
);

check_diagnostics_with_disabled(
r#"
//- minicore: sized
//- minicore: coerce_unsized, dispatch_from_dyn
trait Trait {}

struct Box<T: ?Sized>;
Expand Down Expand Up @@ -476,7 +476,7 @@ fn main() {
fn ptr_to_ptr_different_regions() {
check_diagnostics(
r#"
//- minicore: sized
//- minicore: coerce_unsized, dispatch_from_dyn
struct Foo<'a> { a: &'a () }

fn extend_lifetime_very_very_safely<'a>(v: *const Foo<'a>) -> *const Foo<'static> {
Expand All @@ -503,7 +503,7 @@ fn main() {
fn ptr_to_trait_obj_add_auto() {
check_diagnostics(
r#"
//- minicore: pointee
//- minicore: pointee, coerce_unsized, dispatch_from_dyn
trait Trait<'a> {}

fn add_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send) {
Expand All @@ -522,7 +522,7 @@ fn add_multiple_auto<'a>(x: *mut dyn Trait<'a>) -> *mut (dyn Trait<'a> + Send +
fn ptr_to_trait_obj_add_super_auto() {
check_diagnostics(
r#"
//- minicore: pointee
//- minicore: pointee, coerce_unsized, dispatch_from_dyn
trait Trait: Send {}
impl Trait for () {}

Expand All @@ -538,7 +538,7 @@ fn main() {
fn ptr_to_trait_obj_ok() {
check_diagnostics(
r#"
//- minicore: pointee
//- minicore: pointee, coerce_unsized, dispatch_from_dyn
trait Trait<'a> {}

fn remove_auto<'a>(x: *mut (dyn Trait<'a> + Send)) -> *mut dyn Trait<'a> {
Expand All @@ -560,7 +560,7 @@ fn unprincipled<'a, 'b>(x: *mut (dyn Send + 'a)) -> *mut (dyn Sync + 'b) {
fn ptr_to_trait_obj_wrap_upcast() {
check_diagnostics(
r#"
//- minicore: sized
//- minicore: pointee, coerce_unsized, dispatch_from_dyn
trait Super {}
trait Sub: Super {}

Expand Down Expand Up @@ -1004,4 +1004,16 @@ fn _slice(bar: &[i32]) -> bool {
&["E0308"],
);
}

#[test]
fn cast_into_dst_with_integer_fallback() {
check_diagnostics(
r#"
//- minicore: fmt, coerce_unsized, dispatch_from_dyn
fn test() {
let _ = &42 as &dyn core::fmt::Debug;
}
"#,
)
}
}
Loading