You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Only traits defined in the current crate can be implemented for arbitrary types
8
+
pub(crate)fntrait_impl_orphan(
9
+
ctx:&DiagnosticsContext<'_>,
10
+
d:&hir::TraitImplOrphan,
11
+
) -> Diagnostic{
12
+
Diagnostic::new_with_syntax_node_ptr(
13
+
ctx,
14
+
DiagnosticCode::RustcHardError("E0117"),
15
+
format!("only traits defined in the current crate can be implemented for arbitrary types"),
16
+
InFile::new(d.file_id, d.impl_.clone().into()),
17
+
)
18
+
// Not yet checked for false positives
19
+
.experimental()
20
+
}
21
+
22
+
#[cfg(test)]
23
+
mod tests {
24
+
usecrate::tests::check_diagnostics;
25
+
26
+
#[test]
27
+
fnsimple(){
28
+
check_diagnostics(
29
+
r#"
30
+
//- /foo.rs crate:foo
31
+
pub trait Foo {}
32
+
//- /bar.rs crate:bar
33
+
pub struct Bar;
34
+
//- /main.rs crate:main deps:foo,bar
35
+
struct LocalType;
36
+
trait LocalTrait {}
37
+
impl foo::Foo for bar::Bar {}
38
+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
39
+
impl foo::Foo for LocalType {}
40
+
impl LocalTrait for bar::Bar {}
41
+
"#,
42
+
);
43
+
}
44
+
45
+
#[test]
46
+
fngenerics(){
47
+
check_diagnostics(
48
+
r#"
49
+
//- /foo.rs crate:foo
50
+
pub trait Foo<T> {}
51
+
//- /bar.rs crate:bar
52
+
pub struct Bar<T>(T);
53
+
//- /main.rs crate:main deps:foo,bar
54
+
struct LocalType<T>;
55
+
trait LocalTrait<T> {}
56
+
impl<T> foo::Foo<T> for bar::Bar<T> {}
57
+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
58
+
59
+
impl<T> foo::Foo<T> for bar::Bar<LocalType<T>> {}
60
+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
61
+
62
+
impl<T> foo::Foo<LocalType<T>> for bar::Bar<T> {}
63
+
64
+
impl<T> foo::Foo<bar::Bar<LocalType<T>>> for bar::Bar<LocalType<T>> {}
65
+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
66
+
"#,
67
+
);
68
+
}
69
+
70
+
#[test]
71
+
fnfundamental(){
72
+
check_diagnostics(
73
+
r#"
74
+
//- /foo.rs crate:foo
75
+
pub trait Foo<T> {}
76
+
//- /bar.rs crate:bar
77
+
pub struct Bar<T>(T);
78
+
#[lang = "owned_box"]
79
+
#[fundamental]
80
+
pub struct Box<T>(T);
81
+
//- /main.rs crate:main deps:foo,bar
82
+
struct LocalType;
83
+
impl<T> foo::Foo<T> for bar::Box<T> {}
84
+
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: only traits defined in the current crate can be implemented for arbitrary types
0 commit comments