Unnamed repository; edit this file 'description' to name the repository.
add a test for trait upcasting type mismatch
this adds a test asserting *incorrect* behavior that can be seen in
<https://github.com/rust-lang/rust-analyzer/issues/18083>, and a test
asserting the *correct* behavior for the case of no super traits.
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/type_mismatch.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs index bfdda53740..be2432f44e 100644 --- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs +++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs @@ -1164,6 +1164,38 @@ struct Bar { } #[test] + fn trait_upcast_ok() { + check_diagnostics( + r#" +//- minicore: coerce_unsized +trait A: B {} +trait B {} + +fn test(a: &dyn A) -> &dyn B { + a + //^ error: expected &dyn B, found &dyn A +} +"#, + ); + } + + #[test] + fn trait_upcast_err() { + check_diagnostics( + r#" +//- minicore: coerce_unsized +trait A {} // `A` does not have `B` as a supertrait, so no upcast :c +trait B {} + +fn test(a: &dyn A) -> &dyn B { + a + //^ error: expected &dyn B, found &dyn A +} +"#, + ); + } + + #[test] fn return_no_value() { check_diagnostics_with_disabled( r#" |