Skip to content

Commit fc9e424

Browse files
committed
Auto merge of #26172 - nham:add_E0116, r=alexcrichton
Also improves the wording of the E0133 description. cc #24407
2 parents f6341a8 + aace367 commit fc9e424

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

src/doc/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ methods in such an implementation can only be used as direct calls on the
15501550
values of the type that the implementation targets. In such an implementation,
15511551
the trait type and `for` after `impl` are omitted. Such implementations are
15521552
limited to nominal types (enums, structs), and the implementation must appear
1553-
in the same module or a sub-module as the `self` type:
1553+
in the same crate as the `self` type:
15541554

15551555
```
15561556
struct Point {x: i32, y: i32}

src/librustc/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ http://doc.rust-lang.org/reference.html#ffi-attributes
312312
E0133: r##"
313313
Using unsafe functionality, such as dereferencing raw pointers and calling
314314
functions via FFI or marked as unsafe, is potentially dangerous and disallowed
315-
by safety checks. As such, those safety checks can be temporarily relaxed by
316-
wrapping the unsafe instructions inside an `unsafe` block. For instance:
315+
by safety checks. These safety checks can be relaxed for a section of the code
316+
by wrapping the unsafe instructions with an `unsafe` block. For instance:
317317
318318
```
319319
unsafe fn f() { return; }

src/librustc_typeck/coherence/orphan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ impl<'cx, 'tcx> OrphanChecker<'cx, 'tcx> {
3333
fn check_def_id(&self, item: &ast::Item, def_id: ast::DefId) {
3434
if def_id.krate != ast::LOCAL_CRATE {
3535
span_err!(self.tcx.sess, item.span, E0116,
36-
"cannot associate methods with a type outside the \
37-
crate the type is defined in; define and implement \
36+
"cannot define inherent `impl` for a type outside of the \
37+
crate where the type is defined; define and implement \
3838
a trait or new type instead");
3939
}
4040
}

src/librustc_typeck/diagnostics.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,32 @@ RFC. It is, however, [currently unimplemented][iss15872].
730730
[iss15872]: https://github.com/rust-lang/rust/issues/15872
731731
"##,
732732

733+
E0116: r##"
734+
You can only define an inherent implementation for a type in the same crate
735+
where the type was defined. For example, an `impl` block as below is not allowed
736+
since `Vec` is defined in the standard library:
737+
738+
```
739+
impl Vec<u8> { ... } // error
740+
```
741+
742+
To fix this problem, you can do either of these things:
743+
744+
- define a trait that has the desired associated functions/types/constants and
745+
implement the trait for the type in question
746+
- define a new type wrapping the type and define an implementation on the new
747+
type
748+
749+
Note that using the `type` keyword does not work here because `type` only
750+
introduces a type alias:
751+
752+
```
753+
type Bytes = Vec<u8>;
754+
755+
impl Bytes { ... } // error, same as above
756+
```
757+
"##,
758+
733759
E0121: r##"
734760
In order to be consistent with Rust's lack of global type inference, type
735761
placeholders are disallowed by design in item signatures.
@@ -1232,7 +1258,6 @@ register_diagnostics! {
12321258
E0102,
12331259
E0103,
12341260
E0104,
1235-
E0116,
12361261
E0117,
12371262
E0118,
12381263
E0119,

src/test/compile-fail/trait-or-new-type-instead.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
impl<T> Option<T> {
12-
//~^ ERROR cannot associate methods with a type outside the crate the type is defined in
12+
//~^ ERROR cannot define inherent `impl` for a type outside of the crate where the type is defined
1313
pub fn foo(&self) { }
1414
}
1515

0 commit comments

Comments
 (0)