Skip to content

Commit 033b886

Browse files
committed
Rollup merge of #27313 - nagisa:illegal-to-invalid, r=pnkfelix
Improves diagnostics in various locations, namely: * A few error messages that orignally were a mix of an error message and suggestion how to fix it have been split up into two messages: an error and help/hint. * Never report “illegal”. Fixes #27288
2 parents 10387d6 + cca0ea7 commit 033b886

Some content is hidden

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

48 files changed

+183
-141
lines changed

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,7 @@ impl<'a> LifetimeContext<'a> {
674674
for lifetime in lifetimes {
675675
if special_idents.iter().any(|&i| i.name == lifetime.lifetime.name) {
676676
span_err!(self.sess, lifetime.lifetime.span, E0262,
677-
"illegal lifetime parameter name: `{}`",
678-
lifetime.lifetime.name);
677+
"invalid lifetime parameter name: `{}`", lifetime.lifetime.name);
679678
}
680679
}
681680

src/librustc_trans/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,7 @@ fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
23252325
_ => {
23262326
bcx.tcx().sess.span_bug(
23272327
expr.span,
2328-
&format!("deref invoked on expr of illegal type {:?}",
2328+
&format!("deref invoked on expr of invalid type {:?}",
23292329
datum.ty));
23302330
}
23312331
};

src/librustc_typeck/check/cast.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,21 @@ impl<'tcx> CastCheck<'tcx> {
122122
CastError::NeedViaInt |
123123
CastError::NeedViaUsize => {
124124
fcx.type_error_message(self.span, |actual| {
125-
format!("illegal cast; cast through {} first: `{}` as `{}`",
126-
match e {
127-
CastError::NeedViaPtr => "a raw pointer",
128-
CastError::NeedViaInt => "an integer",
129-
CastError::NeedViaUsize => "a usize",
130-
_ => unreachable!()
131-
},
125+
format!("casting `{}` as `{}` is invalid",
132126
actual,
133127
fcx.infcx().ty_to_string(self.cast_ty))
134-
}, self.expr_ty, None)
128+
}, self.expr_ty, None);
129+
fcx.ccx.tcx.sess.fileline_help(self.span,
130+
&format!("cast through {} first", match e {
131+
CastError::NeedViaPtr => "a raw pointer",
132+
CastError::NeedViaInt => "an integer",
133+
CastError::NeedViaUsize => "a usize",
134+
_ => unreachable!()
135+
}));
135136
}
136137
CastError::CastToBool => {
137-
span_err!(fcx.tcx().sess, self.span, E0054,
138-
"cannot cast as `bool`, compare with zero instead");
138+
span_err!(fcx.tcx().sess, self.span, E0054, "cannot cast as `bool`");
139+
fcx.ccx.tcx.sess.fileline_help(self.span, "compare with zero instead");
139140
}
140141
CastError::CastToChar => {
141142
fcx.type_error_message(self.span, |actual| {
@@ -151,17 +152,18 @@ impl<'tcx> CastCheck<'tcx> {
151152
}
152153
CastError::IllegalCast => {
153154
fcx.type_error_message(self.span, |actual| {
154-
format!("illegal cast: `{}` as `{}`",
155+
format!("casting `{}` as `{}` is invalid",
155156
actual,
156157
fcx.infcx().ty_to_string(self.cast_ty))
157158
}, self.expr_ty, None);
158159
}
159160
CastError::DifferingKinds => {
160161
fcx.type_error_message(self.span, |actual| {
161-
format!("illegal cast: `{}` as `{}`; vtable kinds may not match",
162+
format!("casting `{}` as `{}` is invalid",
162163
actual,
163164
fcx.infcx().ty_to_string(self.cast_ty))
164165
}, self.expr_ty, None);
166+
fcx.ccx.tcx.sess.fileline_note(self.span, "vtable kinds may not match");
165167
}
166168
}
167169
}
@@ -285,7 +287,7 @@ impl<'tcx> CastCheck<'tcx> {
285287
return Ok(CastKind::PtrPtrCast);
286288
}
287289

288-
// sized -> unsized? report illegal cast (don't complain about vtable kinds)
290+
// sized -> unsized? report invalid cast (don't complain about vtable kinds)
289291
if fcx.type_is_known_to_be_sized(m_expr.ty, self.span) {
290292
return Err(CastError::IllegalCast);
291293
}

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,7 +3468,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34683468
let tcx = fcx.tcx();
34693469
if !tcx.expr_is_lval(&**lhs) {
34703470
span_err!(tcx.sess, expr.span, E0070,
3471-
"illegal left-hand side expression");
3471+
"invalid left-hand side expression");
34723472
}
34733473

34743474
let lhs_ty = fcx.expr_ty(&**lhs);
@@ -4273,10 +4273,8 @@ pub fn check_representable(tcx: &ty::ctxt,
42734273
// caught by case 1.
42744274
match rty.is_representable(tcx, sp) {
42754275
ty::SelfRecursive => {
4276-
span_err!(tcx.sess, sp, E0072,
4277-
"illegal recursive {} type; \
4278-
wrap the inner value in a box to make it representable",
4279-
designation);
4276+
span_err!(tcx.sess, sp, E0072, "invalid recursive {} type", designation);
4277+
tcx.sess.fileline_help(sp, "wrap the inner value in a box to make it representable");
42804278
return false
42814279
}
42824280
ty::Representable | ty::ContainsRecursive => (),

src/librustc_typeck/check/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn check_binop_assign<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
5757

5858
let tcx = fcx.tcx();
5959
if !tcx.expr_is_lval(lhs_expr) {
60-
span_err!(tcx.sess, lhs_expr.span, E0067, "illegal left-hand side expression");
60+
span_err!(tcx.sess, lhs_expr.span, E0067, "invalid left-hand side expression");
6161
}
6262

6363
fcx.require_expr_have_sized_type(lhs_expr, traits::AssignmentLhsSized);

src/librustc_typeck/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ the pointer the size of the type would need to be unbounded.
778778
Consider the following erroneous definition of a type for a list of bytes:
779779
780780
```
781-
// error, illegal recursive struct type
781+
// error, invalid recursive struct type
782782
struct ListNode {
783783
head: u8,
784784
tail: Option<ListNode>,
@@ -2362,7 +2362,7 @@ register_diagnostics! {
23622362
E0241,
23632363
E0242, // internal error looking up a definition
23642364
E0245, // not a trait
2365-
E0246, // illegal recursive type
2365+
E0246, // invalid recursive type
23662366
E0247, // found module name used as a type
23672367
E0248, // found value name used as a type
23682368
E0319, // trait impls for defaulted traits allowed just for structs/enums

src/libsyntax/parse/lexer/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ impl<'a> StringReader<'a> {
694694
accum_int *= 16;
695695
accum_int += c.to_digit(16).unwrap_or_else(|| {
696696
self.err_span_char(self.last_pos, self.pos,
697-
"illegal character in numeric character escape", c);
697+
"invalid character in numeric character escape", c);
698698

699699
valid = false;
700700
0
@@ -714,7 +714,7 @@ impl<'a> StringReader<'a> {
714714
Some(_) => valid,
715715
None => {
716716
let last_bpos = self.last_pos;
717-
self.err_span_(start_bpos, last_bpos, "illegal numeric character escape");
717+
self.err_span_(start_bpos, last_bpos, "invalid numeric character escape");
718718
false
719719
}
720720
}
@@ -846,7 +846,7 @@ impl<'a> StringReader<'a> {
846846
"unterminated unicode escape (needed a `}`)");
847847
} else {
848848
self.err_span_char(self.last_pos, self.pos,
849-
"illegal character in unicode escape", c);
849+
"invalid character in unicode escape", c);
850850
}
851851
valid = false;
852852
0
@@ -862,7 +862,7 @@ impl<'a> StringReader<'a> {
862862
}
863863

864864
if valid && (char::from_u32(accum_int).is_none() || count == 0) {
865-
self.err_span_(start_bpos, self.last_pos, "illegal unicode character escape");
865+
self.err_span_(start_bpos, self.last_pos, "invalid unicode character escape");
866866
valid = false;
867867
}
868868

@@ -1138,8 +1138,8 @@ impl<'a> StringReader<'a> {
11381138
let last_bpos = self.last_pos;
11391139
let curr_char = self.curr.unwrap();
11401140
self.fatal_span_char(start_bpos, last_bpos,
1141-
"only `#` is allowed in raw string delimitation; \
1142-
found illegal character",
1141+
"found invalid character; \
1142+
only `#` is allowed in raw string delimitation",
11431143
curr_char);
11441144
}
11451145
self.bump();
@@ -1323,8 +1323,8 @@ impl<'a> StringReader<'a> {
13231323
let last_pos = self.last_pos;
13241324
let ch = self.curr.unwrap();
13251325
self.fatal_span_char(start_bpos, last_pos,
1326-
"only `#` is allowed in raw string delimitation; \
1327-
found illegal character",
1326+
"found invalid character; \
1327+
only `#` is allowed in raw string delimitation",
13281328
ch);
13291329
}
13301330
self.bump();

src/libsyntax/parse/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,11 @@ fn filtered_float_lit(data: token::InternedString, suffix: Option<&str>,
446446
Some(suf) => {
447447
if suf.len() >= 2 && looks_like_width_suffix(&['f'], suf) {
448448
// if it looks like a width, lets try to be helpful.
449-
sd.span_err(sp, &*format!("illegal width `{}` for float literal, \
450-
valid widths are 32 and 64", &suf[1..]));
449+
sd.span_err(sp, &*format!("invalid width `{}` for float literal", &suf[1..]));
450+
sd.fileline_help(sp, "valid widths are 32 and 64");
451451
} else {
452-
sd.span_err(sp, &*format!("illegal suffix `{}` for float literal, \
453-
valid suffixes are `f32` and `f64`", suf));
452+
sd.span_err(sp, &*format!("invalid suffix `{}` for float literal", suf));
453+
sd.fileline_help(sp, "valid suffixes are `f32` and `f64`");
454454
}
455455

456456
ast::LitFloatUnsuffixed(data)
@@ -619,11 +619,11 @@ pub fn integer_lit(s: &str,
619619
// i<digits> and u<digits> look like widths, so lets
620620
// give an error message along those lines
621621
if looks_like_width_suffix(&['i', 'u'], suf) {
622-
sd.span_err(sp, &*format!("illegal width `{}` for integer literal; \
623-
valid widths are 8, 16, 32 and 64",
622+
sd.span_err(sp, &*format!("invalid width `{}` for integer literal",
624623
&suf[1..]));
624+
sd.fileline_help(sp, "valid widths are 8, 16, 32 and 64");
625625
} else {
626-
sd.span_err(sp, &*format!("illegal suffix `{}` for numeric literal", suf));
626+
sd.span_err(sp, &*format!("invalid suffix `{}` for numeric literal", suf));
627627
sd.fileline_help(sp, "the suffix must be one of the integral types \
628628
(`u32`, `isize`, etc)");
629629
}

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ impl<'a> Parser<'a> {
681681
if text.is_empty() {
682682
self.span_bug(sp, "found empty literal suffix in Some")
683683
}
684-
self.span_err(sp, &*format!("{} with a suffix is illegal", kind));
684+
self.span_err(sp, &*format!("{} with a suffix is invalid", kind));
685685
}
686686
}
687687
}
@@ -5286,7 +5286,7 @@ impl<'a> Parser<'a> {
52865286
let last_span = self.last_span;
52875287
self.span_err(
52885288
last_span,
5289-
&format!("illegal ABI: expected one of [{}], \
5289+
&format!("invalid ABI: expected one of [{}], \
52905290
found `{}`",
52915291
abi::all_names().join(", "),
52925292
s));

src/test/compile-fail/bad-expr-lhs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
1 = 2; //~ ERROR illegal left-hand side expression
13-
1 += 2; //~ ERROR illegal left-hand side expression
14-
(1, 2) = (3, 4); //~ ERROR illegal left-hand side expression
12+
1 = 2; //~ ERROR invalid left-hand side expression
13+
1 += 2; //~ ERROR invalid left-hand side expression
14+
(1, 2) = (3, 4); //~ ERROR invalid left-hand side expression
1515

1616
let (a, b) = (1, 2);
17-
(a, b) = (3, 4); //~ ERROR illegal left-hand side expression
17+
(a, b) = (3, 4); //~ ERROR invalid left-hand side expression
1818

19-
None = Some(3); //~ ERROR illegal left-hand side expression
19+
None = Some(3); //~ ERROR invalid left-hand side expression
2020
}

src/test/compile-fail/cast-as-bool.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: cannot cast as `bool`, compare with zero instead
12-
fn main() { let u = (5 as bool); }
11+
fn main() {
12+
let u = (5 as bool);
13+
//~^ ERROR cannot cast as `bool`
14+
//~^^ HELP compare with zero instead
15+
}

src/test/compile-fail/cast-rfc0401.rs

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010

1111
fn illegal_cast<U:?Sized,V:?Sized>(u: *const U) -> *const V
1212
{
13-
u as *const V //~ ERROR vtable kinds
13+
u as *const V
14+
//~^ ERROR casting
15+
//~^^ NOTE vtable kinds
1416
}
1517

1618
fn illegal_cast_2<U:?Sized>(u: *const U) -> *const str
1719
{
18-
u as *const str //~ ERROR vtable kinds
20+
u as *const str
21+
//~^ ERROR casting
22+
//~^^ NOTE vtable kinds
1923
}
2024

2125
trait Foo { fn foo(&self) {} }
@@ -41,32 +45,58 @@ fn main()
4145
let _ = v as (u32,); //~ ERROR non-scalar
4246
let _ = Some(&v) as *const u8; //~ ERROR non-scalar
4347

44-
let _ = v as f32; //~ ERROR through a usize first
45-
let _ = main as f64; //~ ERROR through a usize first
46-
let _ = &v as usize; //~ ERROR through a raw pointer first
47-
let _ = f as *const u8; //~ ERROR through a usize first
48-
let _ = 3 as bool; //~ ERROR compare with zero
49-
let _ = E::A as bool; //~ ERROR compare with zero
48+
let _ = v as f32;
49+
//~^ ERROR casting
50+
//~^^ HELP through a usize first
51+
let _ = main as f64;
52+
//~^ ERROR casting
53+
//~^^ HELP through a usize first
54+
let _ = &v as usize;
55+
//~^ ERROR casting
56+
//~^^ HELP through a raw pointer first
57+
let _ = f as *const u8;
58+
//~^ ERROR casting
59+
//~^^ HELP through a usize first
60+
let _ = 3 as bool;
61+
//~^ ERROR cannot cast as `bool`
62+
//~^^ HELP compare with zero
63+
let _ = E::A as bool;
64+
//~^ ERROR cannot cast as `bool`
65+
//~^^ HELP compare with zero
5066
let _ = 0x61u32 as char; //~ ERROR only `u8` can be cast
5167

52-
let _ = false as f32; //~ ERROR through an integer first
53-
let _ = E::A as f32; //~ ERROR through an integer first
54-
let _ = 'a' as f32; //~ ERROR through an integer first
68+
let _ = false as f32;
69+
//~^ ERROR casting
70+
//~^^ HELP through an integer first
71+
let _ = E::A as f32;
72+
//~^ ERROR casting
73+
//~^^ HELP through an integer first
74+
let _ = 'a' as f32;
75+
//~^ ERROR casting
76+
//~^^ HELP through an integer first
5577

56-
let _ = false as *const u8; //~ ERROR through a usize first
57-
let _ = E::A as *const u8; //~ ERROR through a usize first
58-
let _ = 'a' as *const u8; //~ ERROR through a usize first
78+
let _ = false as *const u8;
79+
//~^ ERROR casting
80+
//~^^ HELP through a usize first
81+
let _ = E::A as *const u8;
82+
//~^ ERROR casting
83+
//~^^ HELP through a usize first
84+
let _ = 'a' as *const u8;
85+
//~^ ERROR casting
86+
//~^^ HELP through a usize first
5987

60-
let _ = 42usize as *const [u8]; //~ ERROR illegal cast
61-
let _ = v as *const [u8]; //~ ERROR illegal cast
88+
let _ = 42usize as *const [u8]; //~ ERROR casting
89+
let _ = v as *const [u8]; //~ ERROR casting
6290
let _ = fat_v as *const Foo;
6391
//~^ ERROR `core::marker::Sized` is not implemented for the type `[u8]`
64-
let _ = foo as *const str; //~ ERROR illegal cast
65-
let _ = foo as *mut str; //~ ERROR illegal cast
66-
let _ = main as *mut str; //~ ERROR illegal cast
67-
let _ = &f as *mut f32; //~ ERROR illegal cast
68-
let _ = &f as *const f64; //~ ERROR illegal cast
69-
let _ = fat_v as usize; //~ ERROR through a raw pointer first
92+
let _ = foo as *const str; //~ ERROR casting
93+
let _ = foo as *mut str; //~ ERROR casting
94+
let _ = main as *mut str; //~ ERROR casting
95+
let _ = &f as *mut f32; //~ ERROR casting
96+
let _ = &f as *const f64; //~ ERROR casting
97+
let _ = fat_v as usize;
98+
//~^ ERROR casting
99+
//~^^ HELP through a raw pointer first
70100

71101
let a : *const str = "hello";
72102
let _ = a as *const Foo;
@@ -76,6 +106,10 @@ fn main()
76106
let _ = main.f as *const u32; //~ ERROR attempted access of field
77107

78108
let cf: *const Foo = &0;
79-
let _ = cf as *const [u8]; //~ ERROR vtable kinds
80-
let _ = cf as *const Bar; //~ ERROR vtable kinds
109+
let _ = cf as *const [u8];
110+
//~^ ERROR casting
111+
//~^^ NOTE vtable kinds
112+
let _ = cf as *const Bar;
113+
//~^ ERROR casting
114+
//~^^ NOTE vtable kinds
81115
}

src/test/compile-fail/const-cast-different-types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
// except according to those terms.
1010

1111
static a: &'static str = "foo";
12-
static b: *const u8 = a as *const u8; //~ ERROR illegal cast
13-
static c: *const u8 = &a as *const u8; //~ ERROR illegal cast
12+
static b: *const u8 = a as *const u8; //~ ERROR casting
13+
static c: *const u8 = &a as *const u8; //~ ERROR casting
1414

1515
fn main() {
1616
}

src/test/compile-fail/enum-to-float-cast-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ enum F {
2121
}
2222

2323
pub fn main() {
24-
let a = E::L0 as f32; //~ ERROR illegal cast
25-
let c = F::H1 as f32; //~ ERROR illegal cast
24+
let a = E::L0 as f32; //~ ERROR casting
25+
let c = F::H1 as f32; //~ ERROR casting
2626
assert_eq!(a, -1.0f32);
2727
assert_eq!(c, -1.0f32);
2828
}

0 commit comments

Comments
 (0)