Skip to content

Commit cb7c60f

Browse files
committed
Auto merge of #42264 - GuillaumeGomez:new-error-codes, r=Susurrus
New error codes Part of #42229.
2 parents 5de0092 + 2969137 commit cb7c60f

File tree

80 files changed

+344
-185
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+344
-185
lines changed

src/librustc_borrowck/borrowck/mod.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
701701
fn bckerr_to_diag(&self, err: &BckError<'tcx>) -> DiagnosticBuilder<'a> {
702702
let span = err.span.clone();
703703

704-
let msg = match err.code {
704+
match err.code {
705705
err_mutbl => {
706706
let descr = match err.cmt.note {
707707
mc::NoteClosureEnv(_) | mc::NoteUpvarRef(_) => {
@@ -725,10 +725,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
725725

726726
match err.cause {
727727
MutabilityViolation => {
728-
format!("cannot assign to {}", descr)
728+
struct_span_err!(self.tcx.sess, span, E0594, "cannot assign to {}", descr)
729729
}
730730
BorrowViolation(euv::ClosureCapture(_)) => {
731-
format!("closure cannot assign to {}", descr)
731+
struct_span_err!(self.tcx.sess, span, E0595,
732+
"closure cannot assign to {}", descr)
732733
}
733734
BorrowViolation(euv::OverloadedOperator) |
734735
BorrowViolation(euv::AddrOf) |
@@ -737,7 +738,8 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
737738
BorrowViolation(euv::AutoUnsafe) |
738739
BorrowViolation(euv::ForLoop) |
739740
BorrowViolation(euv::MatchDiscriminant) => {
740-
format!("cannot borrow {} as mutable", descr)
741+
struct_span_err!(self.tcx.sess, span, E0596,
742+
"cannot borrow {} as mutable", descr)
741743
}
742744
BorrowViolation(euv::ClosureInvocation) => {
743745
span_bug!(err.span,
@@ -752,17 +754,16 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
752754
format!("`{}`", self.loan_path_to_string(&lp))
753755
}
754756
};
755-
format!("{} does not live long enough", msg)
757+
struct_span_err!(self.tcx.sess, span, E0597, "{} does not live long enough", msg)
756758
}
757759
err_borrowed_pointer_too_short(..) => {
758760
let descr = self.cmt_to_path_or_string(&err.cmt);
759-
format!("lifetime of {} is too short to guarantee \
760-
its contents can be safely reborrowed",
761-
descr)
761+
struct_span_err!(self.tcx.sess, span, E0598,
762+
"lifetime of {} is too short to guarantee \
763+
its contents can be safely reborrowed",
764+
descr)
762765
}
763-
};
764-
765-
self.struct_span_err(span, &msg)
766+
}
766767
}
767768

768769
pub fn report_aliasability_violation(&self,
@@ -1169,7 +1170,7 @@ before rustc 1.16, this temporary lived longer - see issue #39283 \
11691170
if kind == ty::ClosureKind::Fn {
11701171
db.span_help(self.tcx.hir.span(upvar_id.closure_expr_id),
11711172
"consider changing this closure to take \
1172-
self by mutable reference");
1173+
self by mutable reference");
11731174
}
11741175
}
11751176
_ => {

src/librustc_borrowck/diagnostics.rs

+54
Original file line numberDiff line numberDiff line change
@@ -1114,9 +1114,63 @@ fn main() {
11141114
```
11151115
"##,
11161116

1117+
E0596: r##"
1118+
This error occurs because you tried to mutably borrow a non-mutable variable.
1119+
1120+
Example of erroneous code:
1121+
1122+
```compile_fail,E0596
1123+
let x = 1;
1124+
let y = &mut x; // error: cannot borrow mutably
1125+
```
1126+
1127+
In here, `x` isn't mutable, so when we try to mutably borrow it in `y`, it
1128+
fails. To fix this error, you need to make `x` mutable:
1129+
1130+
```
1131+
let mut x = 1;
1132+
let y = &mut x; // ok!
1133+
```
1134+
"##,
1135+
1136+
E0597: r##"
1137+
This error occurs because a borrow was made inside a variable which has a
1138+
greater lifetime than the borrowed one.
1139+
1140+
Example of erroneous code:
1141+
1142+
```compile_fail,E0597
1143+
struct Foo<'a> {
1144+
x: Option<&'a u32>,
1145+
}
1146+
1147+
let mut x = Foo { x: None };
1148+
let y = 0;
1149+
x.x = Some(&y); // error: `y` does not live long enough
1150+
```
1151+
1152+
In here, `x` is created before `y` and therefore has a greater lifetime. Always
1153+
keep in mind that values in a scope are dropped in the opposite order they are
1154+
created. So to fix the previous example, just make the `y` lifetime greater than
1155+
the `x`'s one:
1156+
1157+
```
1158+
struct Foo<'a> {
1159+
x: Option<&'a u32>,
1160+
}
1161+
1162+
let y = 0;
1163+
let mut x = Foo { x: None };
1164+
x.x = Some(&y);
1165+
```
1166+
"##,
1167+
11171168
}
11181169

11191170
register_diagnostics! {
11201171
// E0385, // {} in an aliasable location
11211172
E0524, // two closures require unique access to `..` at the same time
1173+
E0594, // cannot assign to {}
1174+
E0595, // closure cannot assign to {}
1175+
E0598, // lifetime of {} is too short to guarantee its contents can be...
11221176
}

src/librustc_typeck/check/method/suggest.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
165165
.. }) => {
166166
let tcx = self.tcx;
167167

168-
let mut err = self.type_error_struct(span,
169-
|actual| {
170-
format!("no {} named `{}` found for type `{}` in the current scope",
171-
if mode == Mode::MethodCall {
172-
"method"
173-
} else {
174-
"associated item"
175-
},
176-
item_name,
177-
actual)
178-
},
179-
rcvr_ty);
168+
let actual = self.resolve_type_vars_if_possible(&rcvr_ty);
169+
let mut err = if !actual.references_error() {
170+
struct_span_err!(tcx.sess, span, E0599,
171+
"no {} named `{}` found for type `{}` in the \
172+
current scope",
173+
if mode == Mode::MethodCall {
174+
"method"
175+
} else {
176+
"associated item"
177+
},
178+
item_name,
179+
self.ty_to_string(actual))
180+
} else {
181+
self.tcx.sess.diagnostic().struct_dummy()
182+
};
180183

181184
// If the method name is the name of a field with a function or closure type,
182185
// give a helping note that it has to be called as (x.f)(...).

src/librustc_typeck/check/op.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
316316
match self.lookup_op_method(ex, operand_ty, vec![], mname, trait_did, operand_expr) {
317317
Ok(t) => t,
318318
Err(()) => {
319-
self.type_error_message(ex.span, |actual| {
320-
format!("cannot apply unary operator `{}` to type `{}`",
321-
op_str, actual)
322-
}, operand_ty);
319+
let actual = self.resolve_type_vars_if_possible(&operand_ty);
320+
if !actual.references_error() {
321+
struct_span_err!(self.tcx.sess, ex.span, E0600,
322+
"cannot apply unary operator `{}` to type `{}`",
323+
op_str, actual).emit();
324+
}
323325
self.tcx.types.err
324326
}
325327
}

src/librustc_typeck/diagnostics.rs

+54-1
Original file line numberDiff line numberDiff line change
@@ -4005,7 +4005,7 @@ details.
40054005
[issue #33685]: https://github.com/rust-lang/rust/issues/33685
40064006
"##,
40074007

4008-
E0582: r##"
4008+
E0582: r##"
40094009
A lifetime appears only in an associated-type binding,
40104010
and not in the input types to the trait.
40114011
@@ -4042,6 +4042,59 @@ details.
40424042
[issue #33685]: https://github.com/rust-lang/rust/issues/33685
40434043
"##,
40444044

4045+
E0599: r##"
4046+
```compile_fail,E0599
4047+
struct Mouth;
4048+
4049+
let x = Mouth;
4050+
x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
4051+
// in the current scope
4052+
```
4053+
"##,
4054+
4055+
E0600: r##"
4056+
An unary operator was used on a type which doesn't implement it.
4057+
4058+
Example of erroneous code:
4059+
4060+
```compile_fail,E0600
4061+
enum Question {
4062+
Yes,
4063+
No,
4064+
}
4065+
4066+
!Question::Yes; // error: cannot apply unary operator `!` to type `Question`
4067+
```
4068+
4069+
In this case, `Question` would need to implement the `std::ops::Not` trait in
4070+
order to be able to use `!` on it. Let's implement it:
4071+
4072+
```
4073+
use std::ops::Not;
4074+
4075+
enum Question {
4076+
Yes,
4077+
No,
4078+
}
4079+
4080+
// We implement the `Not` trait on the enum.
4081+
impl Not for Question {
4082+
type Output = bool;
4083+
4084+
fn not(self) -> bool {
4085+
match self {
4086+
Question::Yes => false, // If the `Answer` is `Yes`, then it
4087+
// returns false.
4088+
Question::No => true, // And here we do the opposite.
4089+
}
4090+
}
4091+
}
4092+
4093+
assert_eq!(!Question::Yes, false);
4094+
assert_eq!(!Question::No, true);
4095+
```
4096+
"##,
4097+
40454098
}
40464099

40474100
register_diagnostics! {

src/test/compile-fail/E0596.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = 1;
13+
let y = &mut x; //~ ERROR E0596
14+
}

src/test/compile-fail/E0597.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo<'a> {
12+
x: Option<&'a u32>,
13+
}
14+
15+
fn main() {
16+
let mut x = Foo { x: None };
17+
let y = 0;
18+
x.x = Some(&y);
19+
} //~ `y` does not live long enough [E0597]

src/test/compile-fail/E0600.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
!"a"; //~ ERROR E0600
13+
}

src/test/ui/codemap_tests/huge_multispan_highlight.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable local variable `x` as mutable
1+
error[E0596]: cannot borrow immutable local variable `x` as mutable
22
--> $DIR/huge_multispan_highlight.rs:100:18
33
|
44
12 | let x = "foo";

src/test/ui/codemap_tests/issue-28308.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot apply unary operator `!` to type `&'static str`
1+
error[E0600]: cannot apply unary operator `!` to type `&'static str`
22
--> $DIR/issue-28308.rs:12:5
33
|
44
12 | assert!("foo");

src/test/ui/did_you_mean/issue-31424.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable argument `self` as mutable
1+
error[E0596]: cannot borrow immutable argument `self` as mutable
22
--> $DIR/issue-31424.rs:17:15
33
|
44
17 | (&mut self).bar();
@@ -7,7 +7,7 @@ error: cannot borrow immutable argument `self` as mutable
77
| try removing `&mut` here
88
| cannot reborrow mutably
99

10-
error: cannot borrow immutable argument `self` as mutable
10+
error[E0596]: cannot borrow immutable argument `self` as mutable
1111
--> $DIR/issue-31424.rs:23:15
1212
|
1313
22 | fn bar(self: &mut Self) {

src/test/ui/did_you_mean/issue-34126.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable argument `self` as mutable
1+
error[E0596]: cannot borrow immutable argument `self` as mutable
22
--> $DIR/issue-34126.rs:16:23
33
|
44
16 | self.run(&mut self);

src/test/ui/did_you_mean/issue-34337.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable local variable `key` as mutable
1+
error[E0596]: cannot borrow immutable local variable `key` as mutable
22
--> $DIR/issue-34337.rs:16:14
33
|
44
16 | get(&mut key);

src/test/ui/did_you_mean/issue-35937.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
error: cannot borrow immutable field `f.v` as mutable
1+
error[E0596]: cannot borrow immutable field `f.v` as mutable
22
--> $DIR/issue-35937.rs:17:5
33
|
44
16 | let f = Foo { v: Vec::new() };
55
| - consider changing this to `mut f`
66
17 | f.v.push("cat".to_string());
77
| ^^^ cannot mutably borrow immutable field
88

9-
error: cannot assign to immutable field `s.x`
9+
error[E0594]: cannot assign to immutable field `s.x`
1010
--> $DIR/issue-35937.rs:26:5
1111
|
1212
25 | let s = S { x: 42 };
1313
| - consider changing this to `mut s`
1414
26 | s.x += 1;
1515
| ^^^^^^^^ cannot mutably borrow immutable field
1616

17-
error: cannot assign to immutable field `s.x`
17+
error[E0594]: cannot assign to immutable field `s.x`
1818
--> $DIR/issue-35937.rs:30:5
1919
|
2020
29 | fn bar(s: S) {

src/test/ui/did_you_mean/issue-37139.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable local variable `x` as mutable
1+
error[E0596]: cannot borrow immutable local variable `x` as mutable
22
--> $DIR/issue-37139.rs:22:23
33
|
44
22 | test(&mut x);

src/test/ui/did_you_mean/issue-38147-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable borrowed content `*self.s` as mutable
1+
error[E0596]: cannot borrow immutable borrowed content `*self.s` as mutable
22
--> $DIR/issue-38147-2.rs:17:9
33
|
44
12 | s: &'a String

src/test/ui/did_you_mean/issue-38147-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot borrow immutable borrowed content `*self.s` as mutable
1+
error[E0596]: cannot borrow immutable borrowed content `*self.s` as mutable
22
--> $DIR/issue-38147-3.rs:17:9
33
|
44
12 | s: &'a String

0 commit comments

Comments
 (0)