Update BARE_TRAIT_OBJECT and ELLIPSIS_INCLUSIVE_RANGE_PATTERNS to errors in Rust 2021
This commit is contained in:
parent
69e1d22ddb
commit
c2d0f1457a
9 changed files with 177 additions and 32 deletions
|
@ -58,6 +58,7 @@ use rustc_session::lint::builtin::{BARE_TRAIT_OBJECTS, MISSING_ABI};
|
|||
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
|
||||
use rustc_session::parse::ParseSess;
|
||||
use rustc_session::Session;
|
||||
use rustc_span::edition::Edition;
|
||||
use rustc_span::hygiene::ExpnId;
|
||||
use rustc_span::source_map::{respan, DesugaringKind};
|
||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||
|
@ -2774,6 +2775,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||
.map(|snippet| snippet.starts_with("#["))
|
||||
.unwrap_or(true);
|
||||
if !is_macro_callsite {
|
||||
if self.sess.edition() < Edition::Edition2021 {
|
||||
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
|
||||
BARE_TRAIT_OBJECTS,
|
||||
id,
|
||||
|
@ -2781,6 +2783,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||
"trait objects without an explicit `dyn` are deprecated",
|
||||
BuiltinLintDiagnostics::BareTraitObject(span, is_global),
|
||||
)
|
||||
} else {
|
||||
let msg = "trait objects must include the `dyn` keyword";
|
||||
let label = "`dyn` keyword should be added before this trait";
|
||||
let mut err = struct_span_err!(self.sess, span, E0782, "{}", msg,);
|
||||
err.span_label(span, label);
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -471,6 +471,8 @@ E0778: include_str!("./error_codes/E0778.md"),
|
|||
E0779: include_str!("./error_codes/E0779.md"),
|
||||
E0780: include_str!("./error_codes/E0780.md"),
|
||||
E0781: include_str!("./error_codes/E0781.md"),
|
||||
E0782: include_str!("./error_codes/E0782.md"),
|
||||
E0783: include_str!("./error_codes/E0783.md"),
|
||||
;
|
||||
// E0006, // merged with E0005
|
||||
// E0008, // cannot bind by-move into a pattern guard
|
||||
|
|
17
compiler/rustc_error_codes/src/error_codes/E0782.md
Normal file
17
compiler/rustc_error_codes/src/error_codes/E0782.md
Normal file
|
@ -0,0 +1,17 @@
|
|||
Trait objects must include the `dyn` keyword.
|
||||
|
||||
Trait objects are a way to call methods on types that are not known until
|
||||
runtime but conform to some trait.
|
||||
|
||||
In the following code the trait object should be formed with
|
||||
`Box<dyn Foo>`, but `dyn` is left off.
|
||||
|
||||
```compile_fail,E0782
|
||||
trait Foo {}
|
||||
fn test(arg: Box<Foo>) {}
|
||||
```
|
||||
|
||||
This makes it harder to see that `arg` is a trait object and not a
|
||||
simply a heap allocated type called `Foo`.
|
||||
|
||||
This used to be allowed before edition 2021, but is now an error.
|
18
compiler/rustc_error_codes/src/error_codes/E0783.md
Normal file
18
compiler/rustc_error_codes/src/error_codes/E0783.md
Normal file
|
@ -0,0 +1,18 @@
|
|||
The range pattern `...` is no longer allowed.
|
||||
|
||||
Older Rust code using previous editions allowed `...` to stand for exclusive
|
||||
ranges which are now signified using `..=`.
|
||||
|
||||
The following code use to compile, but now it now longer does.
|
||||
|
||||
```compile_fail,E0783
|
||||
fn main() {
|
||||
let n = 2u8;
|
||||
match n {
|
||||
...9 => println!("Got a number less than 10),
|
||||
_ => println!("Got a number 10 or more")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To make this code compile replace the `...` with `..=`.
|
|
@ -1699,12 +1699,23 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
|
|||
let suggestion = "use `..=` for an inclusive range";
|
||||
if parenthesise {
|
||||
self.node_id = Some(pat.id);
|
||||
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
|
||||
let end = expr_to_string(&end);
|
||||
let replace = match start {
|
||||
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
|
||||
None => format!("&(..={})", end),
|
||||
};
|
||||
if cx.sess().edition() >= Edition::Edition2021 {
|
||||
let mut err =
|
||||
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
|
||||
err.span_suggestion(
|
||||
pat.span,
|
||||
suggestion,
|
||||
replace,
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
.emit();
|
||||
} else {
|
||||
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
|
||||
lint.build(msg)
|
||||
.span_suggestion(
|
||||
pat.span,
|
||||
|
@ -1714,17 +1725,31 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
|
|||
)
|
||||
.emit();
|
||||
});
|
||||
}
|
||||
} else {
|
||||
let replace = "..=".to_owned();
|
||||
if cx.sess().edition() >= Edition::Edition2021 {
|
||||
let mut err =
|
||||
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
|
||||
err.span_suggestion_short(
|
||||
join,
|
||||
suggestion,
|
||||
replace,
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
.emit();
|
||||
} else {
|
||||
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
|
||||
lint.build(msg)
|
||||
.span_suggestion_short(
|
||||
join,
|
||||
suggestion,
|
||||
"..=".to_owned(),
|
||||
replace,
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
.emit();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
12
src/test/ui/dyn-keyword/dyn-2021-edition-error.rs
Normal file
12
src/test/ui/dyn-keyword/dyn-2021-edition-error.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
// edition:2021
|
||||
|
||||
fn function(x: &SomeTrait, y: Box<SomeTrait>) {
|
||||
//~^ ERROR trait objects must include the `dyn` keyword
|
||||
//~| ERROR trait objects must include the `dyn` keyword
|
||||
let _x: &SomeTrait = todo!();
|
||||
//~^ ERROR trait objects must include the `dyn` keyword
|
||||
}
|
||||
|
||||
trait SomeTrait {}
|
||||
|
||||
fn main() {}
|
21
src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr
Normal file
21
src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr
Normal file
|
@ -0,0 +1,21 @@
|
|||
error[E0782]: trait objects must include the `dyn` keyword
|
||||
--> $DIR/dyn-2021-edition-error.rs:6:14
|
||||
|
|
||||
LL | let _x: &SomeTrait = todo!();
|
||||
| ^^^^^^^^^ `dyn` keyword should be added before this trait
|
||||
|
||||
error[E0782]: trait objects must include the `dyn` keyword
|
||||
--> $DIR/dyn-2021-edition-error.rs:3:17
|
||||
|
|
||||
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
|
||||
| ^^^^^^^^^ `dyn` keyword should be added before this trait
|
||||
|
||||
error[E0782]: trait objects must include the `dyn` keyword
|
||||
--> $DIR/dyn-2021-edition-error.rs:3:35
|
||||
|
|
||||
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
|
||||
| ^^^^^^^^^ `dyn` keyword should be added before this trait
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0782`.
|
14
src/test/ui/range/exclusive-range-patterns-2021.rs
Normal file
14
src/test/ui/range/exclusive-range-patterns-2021.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
// edition:2021
|
||||
|
||||
fn main() {
|
||||
let n = 2;
|
||||
match n {
|
||||
0...3 => {}
|
||||
//~^ ERROR `...` range patterns are deprecated
|
||||
4...10 => {}
|
||||
//~^ ERROR `...` range patterns are deprecated
|
||||
(11...100) => {}
|
||||
//~^ ERROR `...` range patterns are deprecated
|
||||
_ => {}
|
||||
}
|
||||
}
|
27
src/test/ui/range/exclusive-range-patterns-2021.stderr
Normal file
27
src/test/ui/range/exclusive-range-patterns-2021.stderr
Normal file
|
@ -0,0 +1,27 @@
|
|||
error[E0783]: `...` range patterns are deprecated
|
||||
--> $DIR/exclusive-range-patterns-2021.rs:6:9
|
||||
|
|
||||
LL | 0...3 => {}
|
||||
| ^---^
|
||||
| |
|
||||
| help: use `..=` for an inclusive range
|
||||
|
||||
error[E0783]: `...` range patterns are deprecated
|
||||
--> $DIR/exclusive-range-patterns-2021.rs:8:9
|
||||
|
|
||||
LL | 4...10 => {}
|
||||
| ^---^^
|
||||
| |
|
||||
| help: use `..=` for an inclusive range
|
||||
|
||||
error[E0783]: `...` range patterns are deprecated
|
||||
--> $DIR/exclusive-range-patterns-2021.rs:10:10
|
||||
|
|
||||
LL | (11...100) => {}
|
||||
| ^^---^^^
|
||||
| |
|
||||
| help: use `..=` for an inclusive range
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0783`.
|
Loading…
Add table
Add a link
Reference in a new issue