Skip to content

Commit 586a8b2

Browse files
committed
---
yaml --- r: 235800 b: refs/heads/stable c: 29d7147 h: refs/heads/master v: v3
1 parent 557f71c commit 586a8b2

File tree

2 files changed

+87
-38
lines changed

2 files changed

+87
-38
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ refs/heads/tmp: afae2ff723393b3ab4ccffef6ac7c6d1809e2da0
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f
3030
refs/tags/homu-tmp: f859507de8c410b648d934d8f5ec1c52daac971d
3131
refs/tags/1.0.0-beta: 8cbb92b53468ee2b0c2d3eeb8567005953d40828
32-
refs/heads/stable: 950c2d813571ad98730a7f4b1b7c0b679315bd69
32+
refs/heads/stable: 29d7147dda7f7fbca1341a7cad2f67107db49863
3333
refs/tags/1.0.0: 55bd4f8ff2b323f317ae89e254ce87162d52a375
3434
refs/tags/1.1.0: bc3c16f09287e5545c1d3f76b7abd54f2eca868b
3535
refs/tags/1.2.0: f557861f822c34f07270347b94b5280de20a597e

branches/stable/src/librustc/diagnostics.rs

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ fn foo(x: Empty) {
5555
// empty
5656
}
5757
}
58-
5958
```
6059
61-
but this won't:
60+
However, this won't:
6261
6362
```
6463
fn foo(x: Option<String>) {
@@ -71,7 +70,18 @@ fn foo(x: Option<String>) {
7170

7271
E0003: r##"
7372
Not-a-Number (NaN) values cannot be compared for equality and hence can never
74-
match the input to a match expression. To match against NaN values, you should
73+
match the input to a match expression. So, the following will not compile:
74+
75+
```
76+
const NAN: f32 = 0.0 / 0.0;
77+
78+
match number {
79+
NAN => { /* ... */ },
80+
// ...
81+
}
82+
```
83+
84+
To match against NaN values, you should
7585
instead use the `is_nan()` method in a guard, like so:
7686
7787
```
@@ -429,22 +439,20 @@ match 5u32 {
429439
"##,
430440

431441
E0038: r####"
442+
Trait objects like `Box<Trait>` can only be constructed when certain
443+
requirements are satisfied by the trait in question.
432444
433-
Trait objects like `Box<Trait>`, can only be constructed when certain
434-
requirements are obeyed by the trait in question.
435-
436-
Trait objects are a form of dynamic dispatch and use dynamically sized types.
437-
So, for a given trait `Trait`, when `Trait` is treated as a type, as in
438-
`Box<Trait>`, the inner type is "unsized". In such cases the boxed pointer is a
439-
"fat pointer" and contains an extra pointer to a method table for dynamic
440-
dispatch. This design mandates some restrictions on the types of traits that are
441-
allowed to be used in trait objects, which are collectively termed as "object
442-
safety" rules.
445+
Trait objects are a form of dynamic dispatch and use a dynamically sized type
446+
for the inner type. So, for a given trait `Trait`, when `Trait` is treated as a
447+
type, as in `Box<Trait>`, the inner type is "unsized". In such cases the boxed
448+
pointer is a "fat pointer" that contains an extra pointer to a table of methods
449+
(among other things) for dynamic dispatch. This design mandates some
450+
restrictions on the types of traits that are allowed to be used in trait
451+
objects, which are collectively termed as "object safety" rules.
443452
444453
Attempting to create a trait object for a non object-safe trait will trigger
445454
this error.
446455
447-
448456
There are various rules:
449457
450458
### The trait cannot require `Self: Sized`
@@ -463,7 +471,7 @@ trait Foo where Self: Sized {
463471
we cannot create an object of type `Box<Foo>` or `&Foo` since in this case
464472
`Self` would not be `Sized`.
465473
466-
Generally `Self : Sized` is used to indicate that the trait should not be used
474+
Generally, `Self : Sized` is used to indicate that the trait should not be used
467475
as a trait object. If the trait comes from your own crate, consider removing
468476
this restriction.
469477
@@ -475,6 +483,7 @@ This happens when a trait has a method like the following:
475483
trait Trait {
476484
fn foo(&self) -> Self;
477485
}
486+
478487
impl Trait for String {
479488
fn foo(&self) -> Self {
480489
"hi".to_owned()
@@ -488,8 +497,11 @@ impl Trait for u8 {
488497
}
489498
```
490499
491-
In such a case, the compiler cannot predict the return type of `foo()` in a case
492-
like the following:
500+
(Note that `&self` and `&mut self` are okay, it's additional `Self` types which
501+
cause this problem)
502+
503+
In such a case, the compiler cannot predict the return type of `foo()` in a
504+
situation like the following:
493505
494506
```
495507
fn call_foo(x: Box<Trait>) {
@@ -498,8 +510,10 @@ fn call_foo(x: Box<Trait>) {
498510
}
499511
```
500512
501-
If the offending method isn't actually being called on the trait object, you can
502-
add a `where Self: Sized` bound on the method:
513+
If only some methods aren't object-safe, you can add a `where Self: Sized` bound
514+
on them to mark them as explicitly unavailable to trait objects. The
515+
functionality will still be available to all other implementers, including
516+
`Box<Trait>` which is itself sized (assuming you `impl Trait for Box<Trait>`)
503517
504518
```
505519
trait Trait {
@@ -508,10 +522,10 @@ trait Trait {
508522
}
509523
```
510524
511-
Now, `foo()` can no longer be called on the trait object, but you will be
512-
allowed to call other trait methods and construct the trait objects. With such a
513-
bound, one can still call `foo()` on types implementing that trait that aren't
514-
behind trait objects.
525+
Now, `foo()` can no longer be called on a trait object, but you will now be
526+
allowed to make a trait object, and that will be able to call any object-safe
527+
methods". With such a bound, one can still call `foo()` on types implementing
528+
that trait that aren't behind trait objects.
515529
516530
### Method has generic type parameters
517531
@@ -535,10 +549,10 @@ impl Trait for u8 {
535549
// ...
536550
```
537551
538-
at compile time a table of all implementations of `Trait`, containing pointers
539-
to the implementation of `foo()` would be generated.
552+
at compile time each implementation of `Trait` will produce a table containing
553+
the various methods (and other items) related to the implementation.
540554
541-
This works fine, but when we the method gains generic parameters, we can have a
555+
This works fine, but when the method gains generic parameters, we can have a
542556
problem.
543557
544558
Usually, generic parameters get _monomorphized_. For example, if I have
@@ -555,12 +569,14 @@ implementation on-demand. If you call `foo()` with a `bool` parameter, the
555569
compiler will only generate code for `foo::<bool>()`. When we have additional
556570
type parameters, the number of monomorphized implementations the compiler
557571
generates does not grow drastically, since the compiler will only generate an
558-
implementation if the function is called with hard substitutions.
572+
implementation if the function is called with unparametrized substitutions
573+
(i.e., substitutions where none of the substituted types are themselves
574+
parametrized).
559575
560576
However, with trait objects we have to make a table containing _every object
561577
that implements the trait_. Now, if it has type parameters, we need to add
562-
implementations for every type that implements the trait, bloating the table
563-
quickly.
578+
implementations for every type that implements the trait, and there could
579+
theoretically be an infinite number of types.
564580
565581
For example, with
566582
@@ -675,13 +691,46 @@ safe, so they are forbidden when specifying supertraits.
675691
676692
There's no easy fix for this, generally code will need to be refactored so that
677693
you no longer need to derive from `Super<Self>`.
678-
679694
"####,
680695

681696
E0079: r##"
682697
Enum variants which contain no data can be given a custom integer
683698
representation. This error indicates that the value provided is not an
684699
integer literal and is therefore invalid.
700+
701+
For example, in the following code,
702+
703+
```
704+
enum Foo {
705+
Q = "32"
706+
}
707+
```
708+
709+
we try to set the representation to a string.
710+
711+
There's no general fix for this; if you can work with an integer
712+
then just set it to one:
713+
714+
```
715+
enum Foo {
716+
Q = 32
717+
}
718+
```
719+
720+
however if you actually wanted a mapping between variants
721+
and non-integer objects, it may be preferable to use a method with
722+
a match instead:
723+
724+
```
725+
enum Foo { Q }
726+
impl Foo {
727+
fn get_str(&self) -> &'static str {
728+
match *self {
729+
Foo::Q => "32",
730+
}
731+
}
732+
}
733+
```
685734
"##,
686735

687736
E0080: r##"
@@ -704,33 +753,33 @@ https://doc.rust-lang.org/reference.html#ffi-attributes
704753
"##,
705754

706755
E0109: r##"
707-
You tried to give a type parameter to a type which doesn't need it. Erroneous
708-
code example:
756+
You tried to give a type parameter to a type which doesn't need it; for example:
709757
710758
```
711759
type X = u32<i32>; // error: type parameters are not allowed on this type
712760
```
713761
714762
Please check that you used the correct type and recheck its definition. Perhaps
715763
it doesn't need the type parameter.
764+
716765
Example:
717766
718767
```
719-
type X = u32; // ok!
768+
type X = u32; // this compiles
720769
```
721770
"##,
722771

723772
E0110: r##"
724-
You tried to give a lifetime parameter to a type which doesn't need it.
725-
Erroneous code example:
773+
You tried to give a lifetime parameter to a type which doesn't need it; for
774+
example:
726775
727776
```
728777
type X = u32<'static>; // error: lifetime parameters are not allowed on
729778
// this type
730779
```
731780
732-
Please check that you used the correct type and recheck its definition,
733-
perhaps it doesn't need the lifetime parameter. Example:
781+
Please check that the correct type was used and recheck its definition; perhaps
782+
it doesn't need the lifetime parameter. Example:
734783
735784
```
736785
type X = u32; // ok!

0 commit comments

Comments
 (0)