Skip to content

Commit c2d0f14

Browse files
committed
Update BARE_TRAIT_OBJECT and ELLIPSIS_INCLUSIVE_RANGE_PATTERNS to errors in Rust 2021
1 parent 69e1d22 commit c2d0f14

File tree

9 files changed

+177
-32
lines changed

9 files changed

+177
-32
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use rustc_session::lint::builtin::{BARE_TRAIT_OBJECTS, MISSING_ABI};
5858
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
5959
use rustc_session::parse::ParseSess;
6060
use rustc_session::Session;
61+
use rustc_span::edition::Edition;
6162
use rustc_span::hygiene::ExpnId;
6263
use rustc_span::source_map::{respan, DesugaringKind};
6364
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -2774,13 +2775,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27742775
.map(|snippet| snippet.starts_with("#["))
27752776
.unwrap_or(true);
27762777
if !is_macro_callsite {
2777-
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2778-
BARE_TRAIT_OBJECTS,
2779-
id,
2780-
span,
2781-
"trait objects without an explicit `dyn` are deprecated",
2782-
BuiltinLintDiagnostics::BareTraitObject(span, is_global),
2783-
)
2778+
if self.sess.edition() < Edition::Edition2021 {
2779+
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2780+
BARE_TRAIT_OBJECTS,
2781+
id,
2782+
span,
2783+
"trait objects without an explicit `dyn` are deprecated",
2784+
BuiltinLintDiagnostics::BareTraitObject(span, is_global),
2785+
)
2786+
} else {
2787+
let msg = "trait objects must include the `dyn` keyword";
2788+
let label = "`dyn` keyword should be added before this trait";
2789+
let mut err = struct_span_err!(self.sess, span, E0782, "{}", msg,);
2790+
err.span_label(span, label);
2791+
err.emit();
2792+
}
27842793
}
27852794
}
27862795

compiler/rustc_error_codes/src/error_codes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ E0778: include_str!("./error_codes/E0778.md"),
471471
E0779: include_str!("./error_codes/E0779.md"),
472472
E0780: include_str!("./error_codes/E0780.md"),
473473
E0781: include_str!("./error_codes/E0781.md"),
474+
E0782: include_str!("./error_codes/E0782.md"),
475+
E0783: include_str!("./error_codes/E0783.md"),
474476
;
475477
// E0006, // merged with E0005
476478
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Trait objects must include the `dyn` keyword.
2+
3+
Trait objects are a way to call methods on types that are not known until
4+
runtime but conform to some trait.
5+
6+
In the following code the trait object should be formed with
7+
`Box<dyn Foo>`, but `dyn` is left off.
8+
9+
```compile_fail,E0782
10+
trait Foo {}
11+
fn test(arg: Box<Foo>) {}
12+
```
13+
14+
This makes it harder to see that `arg` is a trait object and not a
15+
simply a heap allocated type called `Foo`.
16+
17+
This used to be allowed before edition 2021, but is now an error.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The range pattern `...` is no longer allowed.
2+
3+
Older Rust code using previous editions allowed `...` to stand for exclusive
4+
ranges which are now signified using `..=`.
5+
6+
The following code use to compile, but now it now longer does.
7+
8+
```compile_fail,E0783
9+
fn main() {
10+
let n = 2u8;
11+
match n {
12+
...9 => println!("Got a number less than 10),
13+
_ => println!("Got a number 10 or more")
14+
}
15+
}
16+
```
17+
18+
To make this code compile replace the `...` with `..=`.

compiler/rustc_lint/src/builtin.rs

+50-25
Original file line numberDiff line numberDiff line change
@@ -1699,32 +1699,57 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
16991699
let suggestion = "use `..=` for an inclusive range";
17001700
if parenthesise {
17011701
self.node_id = Some(pat.id);
1702-
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
1703-
let end = expr_to_string(&end);
1704-
let replace = match start {
1705-
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
1706-
None => format!("&(..={})", end),
1707-
};
1708-
lint.build(msg)
1709-
.span_suggestion(
1710-
pat.span,
1711-
suggestion,
1712-
replace,
1713-
Applicability::MachineApplicable,
1714-
)
1715-
.emit();
1716-
});
1702+
let end = expr_to_string(&end);
1703+
let replace = match start {
1704+
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
1705+
None => format!("&(..={})", end),
1706+
};
1707+
if cx.sess().edition() >= Edition::Edition2021 {
1708+
let mut err =
1709+
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
1710+
err.span_suggestion(
1711+
pat.span,
1712+
suggestion,
1713+
replace,
1714+
Applicability::MachineApplicable,
1715+
)
1716+
.emit();
1717+
} else {
1718+
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
1719+
lint.build(msg)
1720+
.span_suggestion(
1721+
pat.span,
1722+
suggestion,
1723+
replace,
1724+
Applicability::MachineApplicable,
1725+
)
1726+
.emit();
1727+
});
1728+
}
17171729
} else {
1718-
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
1719-
lint.build(msg)
1720-
.span_suggestion_short(
1721-
join,
1722-
suggestion,
1723-
"..=".to_owned(),
1724-
Applicability::MachineApplicable,
1725-
)
1726-
.emit();
1727-
});
1730+
let replace = "..=".to_owned();
1731+
if cx.sess().edition() >= Edition::Edition2021 {
1732+
let mut err =
1733+
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
1734+
err.span_suggestion_short(
1735+
join,
1736+
suggestion,
1737+
replace,
1738+
Applicability::MachineApplicable,
1739+
)
1740+
.emit();
1741+
} else {
1742+
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
1743+
lint.build(msg)
1744+
.span_suggestion_short(
1745+
join,
1746+
suggestion,
1747+
replace,
1748+
Applicability::MachineApplicable,
1749+
)
1750+
.emit();
1751+
});
1752+
}
17281753
};
17291754
}
17301755
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// edition:2021
2+
3+
fn function(x: &SomeTrait, y: Box<SomeTrait>) {
4+
//~^ ERROR trait objects must include the `dyn` keyword
5+
//~| ERROR trait objects must include the `dyn` keyword
6+
let _x: &SomeTrait = todo!();
7+
//~^ ERROR trait objects must include the `dyn` keyword
8+
}
9+
10+
trait SomeTrait {}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0782]: trait objects must include the `dyn` keyword
2+
--> $DIR/dyn-2021-edition-error.rs:6:14
3+
|
4+
LL | let _x: &SomeTrait = todo!();
5+
| ^^^^^^^^^ `dyn` keyword should be added before this trait
6+
7+
error[E0782]: trait objects must include the `dyn` keyword
8+
--> $DIR/dyn-2021-edition-error.rs:3:17
9+
|
10+
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
11+
| ^^^^^^^^^ `dyn` keyword should be added before this trait
12+
13+
error[E0782]: trait objects must include the `dyn` keyword
14+
--> $DIR/dyn-2021-edition-error.rs:3:35
15+
|
16+
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
17+
| ^^^^^^^^^ `dyn` keyword should be added before this trait
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0782`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// edition:2021
2+
3+
fn main() {
4+
let n = 2;
5+
match n {
6+
0...3 => {}
7+
//~^ ERROR `...` range patterns are deprecated
8+
4...10 => {}
9+
//~^ ERROR `...` range patterns are deprecated
10+
(11...100) => {}
11+
//~^ ERROR `...` range patterns are deprecated
12+
_ => {}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0783]: `...` range patterns are deprecated
2+
--> $DIR/exclusive-range-patterns-2021.rs:6:9
3+
|
4+
LL | 0...3 => {}
5+
| ^---^
6+
| |
7+
| help: use `..=` for an inclusive range
8+
9+
error[E0783]: `...` range patterns are deprecated
10+
--> $DIR/exclusive-range-patterns-2021.rs:8:9
11+
|
12+
LL | 4...10 => {}
13+
| ^---^^
14+
| |
15+
| help: use `..=` for an inclusive range
16+
17+
error[E0783]: `...` range patterns are deprecated
18+
--> $DIR/exclusive-range-patterns-2021.rs:10:10
19+
|
20+
LL | (11...100) => {}
21+
| ^^---^^^
22+
| |
23+
| help: use `..=` for an inclusive range
24+
25+
error: aborting due to 3 previous errors
26+
27+
For more information about this error, try `rustc --explain E0783`.

0 commit comments

Comments
 (0)