Skip to content

Commit a0707ab

Browse files
committed
Exclude final methods when checking dyn-compatibility
1 parent f0df197 commit a0707ab

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ pub fn dyn_compatibility_violations_for_assoc_item(
314314
trait_def_id: DefId,
315315
item: ty::AssocItem,
316316
) -> Vec<DynCompatibilityViolation> {
317+
// `final` assoc functions don't prevent a trait from being dyn-compatible
318+
if tcx.defaultness(item.def_id).is_final() {
319+
return Vec::new();
320+
}
321+
317322
// Any item that has a `Self: Sized` requisite is otherwise exempt from the regulations.
318323
if tcx.generics_require_sized_self(item.def_id) {
319324
return Vec::new();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
3+
#![feature(final_associated_functions)]
4+
5+
trait Foo {
6+
final fn bar<T: std::fmt::Debug>(&self, t: T) {
7+
println!("{:?}", t);
8+
}
9+
10+
fn baz(&self) {}
11+
}
12+
13+
impl Foo for () {}
14+
impl Foo for i32 {}
15+
16+
fn foo(x: &dyn Foo) {
17+
x.bar(42);
18+
x.baz();
19+
}
20+
21+
fn main() {
22+
foo(&());
23+
foo(&42);
24+
}

0 commit comments

Comments
 (0)