From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- .../rustc_error_codes/src/error_codes/E0117.md | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 compiler/rustc_error_codes/src/error_codes/E0117.md (limited to 'compiler/rustc_error_codes/src/error_codes/E0117.md') diff --git a/compiler/rustc_error_codes/src/error_codes/E0117.md b/compiler/rustc_error_codes/src/error_codes/E0117.md new file mode 100644 index 000000000..0544667cc --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0117.md @@ -0,0 +1,50 @@ +Only traits defined in the current crate can be implemented for arbitrary types. + +Erroneous code example: + +```compile_fail,E0117 +impl Drop for u32 {} +``` + +This error indicates a violation of one of Rust's orphan rules for trait +implementations. The rule prohibits any implementation of a foreign trait (a +trait defined in another crate) where + + - the type that is implementing the trait is foreign + - all of the parameters being passed to the trait (if there are any) are also + foreign. + +To avoid this kind of error, ensure that at least one local type is referenced +by the `impl`: + +``` +pub struct Foo; // you define your type in your crate + +impl Drop for Foo { // and you can implement the trait on it! + // code of trait implementation here +# fn drop(&mut self) { } +} + +impl From for i32 { // or you use a type from your crate as + // a type parameter + fn from(i: Foo) -> i32 { + 0 + } +} +``` + +Alternatively, define a trait locally and implement that instead: + +``` +trait Bar { + fn get(&self) -> usize; +} + +impl Bar for u32 { + fn get(&self) -> usize { 0 } +} +``` + +For information on the design of the orphan rules, see [RFC 1023]. + +[RFC 1023]: https://github.com/rust-lang/rfcs/blob/master/text/1023-rebalancing-coherence.md -- cgit v1.2.3