Skip to content

Commit 772266a

Browse files
committed
add a test for trait upcasting type mismatch
this adds a test asserting *incorrect* behavior that can be seen in <#18083>, and a test asserting the *correct* behavior for the case of no super traits.
1 parent 7d337c7 commit 772266a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,38 @@ struct Bar {
11631163
);
11641164
}
11651165

1166+
#[test]
1167+
fn trait_upcast_ok() {
1168+
check_diagnostics(
1169+
r#"
1170+
//- minicore: coerce_unsized
1171+
trait A: B {}
1172+
trait B {}
1173+
1174+
fn test(a: &dyn A) -> &dyn B {
1175+
a
1176+
//^ error: expected &dyn B, found &dyn A
1177+
}
1178+
"#,
1179+
);
1180+
}
1181+
1182+
#[test]
1183+
fn trait_upcast_err() {
1184+
check_diagnostics(
1185+
r#"
1186+
//- minicore: coerce_unsized
1187+
trait A {} // `A` does not have `B` as a supertrait, so no upcast :c
1188+
trait B {}
1189+
1190+
fn test(a: &dyn A) -> &dyn B {
1191+
a
1192+
//^ error: expected &dyn B, found &dyn A
1193+
}
1194+
"#,
1195+
);
1196+
}
1197+
11661198
#[test]
11671199
fn return_no_value() {
11681200
check_diagnostics_with_disabled(

0 commit comments

Comments
 (0)