diff options
| author | Ryan Levick <me@ryanlevick.com> | 2021-03-16 21:47:06 +0100 |
|---|---|---|
| committer | Ryan Levick <me@ryanlevick.com> | 2021-04-08 15:36:27 +0200 |
| commit | c2d0f1457ac71342fb6411ecf6d8253a04686dc1 (patch) | |
| tree | d8f65dd10ae5709701aa2ac89c4dbba2e745959f | |
| parent | 69e1d22ddbc67b25141a735a22a8895a678b32ca (diff) | |
| download | rust-c2d0f1457ac71342fb6411ecf6d8253a04686dc1.tar.gz rust-c2d0f1457ac71342fb6411ecf6d8253a04686dc1.zip | |
Update BARE_TRAIT_OBJECT and ELLIPSIS_INCLUSIVE_RANGE_PATTERNS to errors in Rust 2021
| -rw-r--r-- | compiler/rustc_ast_lowering/src/lib.rs | 23 | ||||
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0782.md | 17 | ||||
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0783.md | 18 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/builtin.rs | 75 | ||||
| -rw-r--r-- | src/test/ui/dyn-keyword/dyn-2021-edition-error.rs | 12 | ||||
| -rw-r--r-- | src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr | 21 | ||||
| -rw-r--r-- | src/test/ui/range/exclusive-range-patterns-2021.rs | 14 | ||||
| -rw-r--r-- | src/test/ui/range/exclusive-range-patterns-2021.stderr | 27 |
9 files changed, 177 insertions, 32 deletions
diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index be56f97af8a..fa32645289c 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -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,13 +2775,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { .map(|snippet| snippet.starts_with("#[")) .unwrap_or(true); if !is_macro_callsite { - self.resolver.lint_buffer().buffer_lint_with_diagnostic( - BARE_TRAIT_OBJECTS, - id, - span, - "trait objects without an explicit `dyn` are deprecated", - BuiltinLintDiagnostics::BareTraitObject(span, is_global), - ) + if self.sess.edition() < Edition::Edition2021 { + self.resolver.lint_buffer().buffer_lint_with_diagnostic( + BARE_TRAIT_OBJECTS, + id, + span, + "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(); + } } } diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index 4b529734328..ab7a13dee69 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -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 diff --git a/compiler/rustc_error_codes/src/error_codes/E0782.md b/compiler/rustc_error_codes/src/error_codes/E0782.md new file mode 100644 index 00000000000..e001aa8bc9b --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0782.md @@ -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. diff --git a/compiler/rustc_error_codes/src/error_codes/E0783.md b/compiler/rustc_error_codes/src/error_codes/E0783.md new file mode 100644 index 00000000000..cc904543b0d --- /dev/null +++ b/compiler/rustc_error_codes/src/error_codes/E0783.md @@ -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 `..=`. diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index b9de144b0eb..9f1efe980f6 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -1699,32 +1699,57 @@ 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), - }; - lint.build(msg) - .span_suggestion( - pat.span, - suggestion, - replace, - Applicability::MachineApplicable, - ) - .emit(); - }); + 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, + 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(), - Applicability::MachineApplicable, - ) - .emit(); - }); + 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, + replace, + Applicability::MachineApplicable, + ) + .emit(); + }); + } }; } } diff --git a/src/test/ui/dyn-keyword/dyn-2021-edition-error.rs b/src/test/ui/dyn-keyword/dyn-2021-edition-error.rs new file mode 100644 index 00000000000..bc1bed8a9a4 --- /dev/null +++ b/src/test/ui/dyn-keyword/dyn-2021-edition-error.rs @@ -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() {} diff --git a/src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr b/src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr new file mode 100644 index 00000000000..791ff6979cb --- /dev/null +++ b/src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr @@ -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`. diff --git a/src/test/ui/range/exclusive-range-patterns-2021.rs b/src/test/ui/range/exclusive-range-patterns-2021.rs new file mode 100644 index 00000000000..de69c9bf2f3 --- /dev/null +++ b/src/test/ui/range/exclusive-range-patterns-2021.rs @@ -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 + _ => {} + } +} diff --git a/src/test/ui/range/exclusive-range-patterns-2021.stderr b/src/test/ui/range/exclusive-range-patterns-2021.stderr new file mode 100644 index 00000000000..a967437041a --- /dev/null +++ b/src/test/ui/range/exclusive-range-patterns-2021.stderr @@ -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`. |
