1
Fork 0

Update BARE_TRAIT_OBJECT and ELLIPSIS_INCLUSIVE_RANGE_PATTERNS to errors in Rust 2021

This commit is contained in:
Ryan Levick 2021-03-16 21:47:06 +01:00
parent 69e1d22ddb
commit c2d0f1457a
9 changed files with 177 additions and 32 deletions

View file

@ -58,6 +58,7 @@ use rustc_session::lint::builtin::{BARE_TRAIT_OBJECTS, MISSING_ABI};
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer}; use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::hygiene::ExpnId; use rustc_span::hygiene::ExpnId;
use rustc_span::source_map::{respan, DesugaringKind}; use rustc_span::source_map::{respan, DesugaringKind};
use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::symbol::{kw, sym, Ident, Symbol};
@ -2774,13 +2775,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
.map(|snippet| snippet.starts_with("#[")) .map(|snippet| snippet.starts_with("#["))
.unwrap_or(true); .unwrap_or(true);
if !is_macro_callsite { if !is_macro_callsite {
self.resolver.lint_buffer().buffer_lint_with_diagnostic( if self.sess.edition() < Edition::Edition2021 {
BARE_TRAIT_OBJECTS, self.resolver.lint_buffer().buffer_lint_with_diagnostic(
id, BARE_TRAIT_OBJECTS,
span, id,
"trait objects without an explicit `dyn` are deprecated", span,
BuiltinLintDiagnostics::BareTraitObject(span, is_global), "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();
}
} }
} }

View file

@ -471,6 +471,8 @@ E0778: include_str!("./error_codes/E0778.md"),
E0779: include_str!("./error_codes/E0779.md"), E0779: include_str!("./error_codes/E0779.md"),
E0780: include_str!("./error_codes/E0780.md"), E0780: include_str!("./error_codes/E0780.md"),
E0781: include_str!("./error_codes/E0781.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 // E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard // E0008, // cannot bind by-move into a pattern guard

View 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.

View 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 `..=`.

View file

@ -1699,32 +1699,57 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
let suggestion = "use `..=` for an inclusive range"; let suggestion = "use `..=` for an inclusive range";
if parenthesise { if parenthesise {
self.node_id = Some(pat.id); self.node_id = Some(pat.id);
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| { let end = expr_to_string(&end);
let end = expr_to_string(&end); let replace = match start {
let replace = match start { Some(start) => format!("&({}..={})", expr_to_string(&start), end),
Some(start) => format!("&({}..={})", expr_to_string(&start), end), None => format!("&(..={})", end),
None => format!("&(..={})", end), };
}; if cx.sess().edition() >= Edition::Edition2021 {
lint.build(msg) let mut err =
.span_suggestion( rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
pat.span, err.span_suggestion(
suggestion, pat.span,
replace, suggestion,
Applicability::MachineApplicable, replace,
) Applicability::MachineApplicable,
.emit(); )
}); .emit();
} else {
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
lint.build(msg)
.span_suggestion(
pat.span,
suggestion,
replace,
Applicability::MachineApplicable,
)
.emit();
});
}
} else { } else {
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| { let replace = "..=".to_owned();
lint.build(msg) if cx.sess().edition() >= Edition::Edition2021 {
.span_suggestion_short( let mut err =
join, rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
suggestion, err.span_suggestion_short(
"..=".to_owned(), join,
Applicability::MachineApplicable, suggestion,
) replace,
.emit(); Applicability::MachineApplicable,
}); )
.emit();
} else {
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
lint.build(msg)
.span_suggestion_short(
join,
suggestion,
replace,
Applicability::MachineApplicable,
)
.emit();
});
}
}; };
} }
} }

View 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() {}

View 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`.

View 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
_ => {}
}
}

View 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`.