Skip to content

Commit 7a0f178

Browse files
committed
Auto merge of #83213 - rylev:update-lints-to-errors, r=nikomatsakis
Update BARE_TRAIT_OBJECT and ELLIPSIS_INCLUSIVE_RANGE_PATTERNS to errors in Rust 2021 This addresses #81244 by updating two lints to errors in the Rust 2021 edition. r? `@estebank`
2 parents c20c921 + 7a50392 commit 7a0f178

File tree

48 files changed

+541
-196
lines changed

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

+541
-196
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use rustc_ast_pretty::pprust;
4646
use rustc_data_structures::captures::Captures;
4747
use rustc_data_structures::fx::FxHashSet;
4848
use rustc_data_structures::sync::Lrc;
49-
use rustc_errors::struct_span_err;
49+
use rustc_errors::{struct_span_err, Applicability};
5050
use rustc_hir as hir;
5151
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
5252
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
@@ -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::utils::{FlattenNonterminals, NtToTokenstream};
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};
@@ -2730,13 +2731,26 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27302731
.map(|snippet| snippet.starts_with("#["))
27312732
.unwrap_or(true);
27322733
if !is_macro_callsite {
2733-
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2734-
BARE_TRAIT_OBJECTS,
2735-
id,
2736-
span,
2737-
"trait objects without an explicit `dyn` are deprecated",
2738-
BuiltinLintDiagnostics::BareTraitObject(span, is_global),
2739-
)
2734+
if span.edition() < Edition::Edition2021 {
2735+
self.resolver.lint_buffer().buffer_lint_with_diagnostic(
2736+
BARE_TRAIT_OBJECTS,
2737+
id,
2738+
span,
2739+
"trait objects without an explicit `dyn` are deprecated",
2740+
BuiltinLintDiagnostics::BareTraitObject(span, is_global),
2741+
)
2742+
} else {
2743+
let msg = "trait objects must include the `dyn` keyword";
2744+
let label = "add `dyn` keyword before this trait";
2745+
let mut err = struct_span_err!(self.sess, span, E0782, "{}", msg,);
2746+
err.span_suggestion_verbose(
2747+
span.shrink_to_lo(),
2748+
label,
2749+
String::from("dyn "),
2750+
Applicability::MachineApplicable,
2751+
);
2752+
err.emit();
2753+
}
27402754
}
27412755
}
27422756

compiler/rustc_error_codes/src/error_codes.rs

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

compiler/rustc_lint/src/builtin.rs

+55-26
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,11 @@ declare_lint! {
16601660
/// [`..` range expression]: https://doc.rust-lang.org/reference/expressions/range-expr.html
16611661
pub ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
16621662
Warn,
1663-
"`...` range patterns are deprecated"
1663+
"`...` range patterns are deprecated",
1664+
@future_incompatible = FutureIncompatibleInfo {
1665+
reference: "issue #80165 <https://github.com/rust-lang/rust/issues/80165>",
1666+
edition: Some(Edition::Edition2021),
1667+
};
16641668
}
16651669

16661670
#[derive(Default)]
@@ -1704,32 +1708,57 @@ impl EarlyLintPass for EllipsisInclusiveRangePatterns {
17041708
let suggestion = "use `..=` for an inclusive range";
17051709
if parenthesise {
17061710
self.node_id = Some(pat.id);
1707-
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
1708-
let end = expr_to_string(&end);
1709-
let replace = match start {
1710-
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
1711-
None => format!("&(..={})", end),
1712-
};
1713-
lint.build(msg)
1714-
.span_suggestion(
1715-
pat.span,
1716-
suggestion,
1717-
replace,
1718-
Applicability::MachineApplicable,
1719-
)
1720-
.emit();
1721-
});
1711+
let end = expr_to_string(&end);
1712+
let replace = match start {
1713+
Some(start) => format!("&({}..={})", expr_to_string(&start), end),
1714+
None => format!("&(..={})", end),
1715+
};
1716+
if join.edition() >= Edition::Edition2021 {
1717+
let mut err =
1718+
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
1719+
err.span_suggestion(
1720+
pat.span,
1721+
suggestion,
1722+
replace,
1723+
Applicability::MachineApplicable,
1724+
)
1725+
.emit();
1726+
} else {
1727+
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, pat.span, |lint| {
1728+
lint.build(msg)
1729+
.span_suggestion(
1730+
pat.span,
1731+
suggestion,
1732+
replace,
1733+
Applicability::MachineApplicable,
1734+
)
1735+
.emit();
1736+
});
1737+
}
17221738
} else {
1723-
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
1724-
lint.build(msg)
1725-
.span_suggestion_short(
1726-
join,
1727-
suggestion,
1728-
"..=".to_owned(),
1729-
Applicability::MachineApplicable,
1730-
)
1731-
.emit();
1732-
});
1739+
let replace = "..=".to_owned();
1740+
if join.edition() >= Edition::Edition2021 {
1741+
let mut err =
1742+
rustc_errors::struct_span_err!(cx.sess, pat.span, E0783, "{}", msg,);
1743+
err.span_suggestion_short(
1744+
join,
1745+
suggestion,
1746+
replace,
1747+
Applicability::MachineApplicable,
1748+
)
1749+
.emit();
1750+
} else {
1751+
cx.struct_span_lint(ELLIPSIS_INCLUSIVE_RANGE_PATTERNS, join, |lint| {
1752+
lint.build(msg)
1753+
.span_suggestion_short(
1754+
join,
1755+
suggestion,
1756+
replace,
1757+
Applicability::MachineApplicable,
1758+
)
1759+
.emit();
1760+
});
1761+
}
17331762
};
17341763
}
17351764
}

compiler/rustc_lint_defs/src/builtin.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,11 @@ declare_lint! {
16181618
/// [`impl Trait`]: https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
16191619
pub BARE_TRAIT_OBJECTS,
16201620
Warn,
1621-
"suggest using `dyn Trait` for trait objects"
1621+
"suggest using `dyn Trait` for trait objects",
1622+
@future_incompatible = FutureIncompatibleInfo {
1623+
reference: "issue #80165 <https://github.com/rust-lang/rust/issues/80165>",
1624+
edition: Some(Edition::Edition2021),
1625+
};
16221626
}
16231627

16241628
declare_lint! {

compiler/rustc_middle/src/lint.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ pub fn struct_lint_level<'s, 'd>(
272272
// emit shouldn't be automatically fixed by rustfix.
273273
err.allow_suggestions(false);
274274

275-
// If this is a future incompatible lint it'll become a hard error, so
276-
// we have to emit *something*. Also, if this lint occurs in the
277-
// expansion of a macro from an external crate, allow individual lints
278-
// to opt-out from being reported.
279-
if future_incompatible.is_none() && !lint.report_in_external_macro {
275+
// If this is a future incompatible that is not an edition fixing lint
276+
// it'll become a hard error, so we have to emit *something*. Also,
277+
// if this lint occurs in the expansion of a macro from an external crate,
278+
// allow individual lints to opt-out from being reported.
279+
let not_future_incompatible =
280+
future_incompatible.map(|f| f.edition.is_some()).unwrap_or(true);
281+
if not_future_incompatible && !lint.report_in_external_macro {
280282
err.cancel();
281283
// Don't continue further, since we don't want to have
282284
// `diag_span_note_once` called for a diagnostic that isn't emitted.

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+37-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use rustc_middle::ty::{
3030
use rustc_session::lint;
3131
use rustc_session::lint::builtin::BARE_TRAIT_OBJECTS;
3232
use rustc_session::parse::feature_err;
33+
use rustc_span::edition::Edition;
3334
use rustc_span::source_map::{original_sp, DUMMY_SP};
3435
use rustc_span::symbol::{kw, sym, Ident};
3536
use rustc_span::{self, BytePos, MultiSpan, Span};
@@ -969,20 +970,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
969970
if let TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
970971
self_ty.kind
971972
{
972-
self.tcx.struct_span_lint_hir(BARE_TRAIT_OBJECTS, hir_id, self_ty.span, |lint| {
973-
let mut db = lint
974-
.build(&format!("trait objects without an explicit `dyn` are deprecated"));
975-
let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(self_ty.span)
976-
{
977-
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
978-
(format!("<dyn ({})>", s), Applicability::MachineApplicable)
979-
}
980-
Ok(s) => (format!("<dyn {}>", s), Applicability::MachineApplicable),
981-
Err(_) => ("<dyn <type>>".to_string(), Applicability::HasPlaceholders),
982-
};
983-
db.span_suggestion(self_ty.span, "use `dyn`", sugg, app);
984-
db.emit()
985-
});
973+
let msg = "trait objects without an explicit `dyn` are deprecated";
974+
let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(self_ty.span) {
975+
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
976+
(format!("<dyn ({})>", s), Applicability::MachineApplicable)
977+
}
978+
Ok(s) => (format!("<dyn {}>", s), Applicability::MachineApplicable),
979+
Err(_) => ("<dyn <type>>".to_string(), Applicability::HasPlaceholders),
980+
};
981+
let replace = String::from("use `dyn`");
982+
if self.sess().edition() >= Edition::Edition2021 {
983+
let mut err = rustc_errors::struct_span_err!(
984+
self.sess(),
985+
self_ty.span,
986+
E0783,
987+
"{}",
988+
msg,
989+
);
990+
err.span_suggestion(
991+
self_ty.span,
992+
&sugg,
993+
replace,
994+
Applicability::MachineApplicable,
995+
)
996+
.emit();
997+
} else {
998+
self.tcx.struct_span_lint_hir(
999+
BARE_TRAIT_OBJECTS,
1000+
hir_id,
1001+
self_ty.span,
1002+
|lint| {
1003+
let mut db = lint.build(msg);
1004+
db.span_suggestion(self_ty.span, &replace, sugg, app);
1005+
db.emit()
1006+
},
1007+
);
1008+
}
9861009
}
9871010
}
9881011
}

src/bootstrap/test.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,9 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) ->
17091709
builder.info(&format!("doc tests for: {}", markdown.display()));
17101710
let mut cmd = builder.rustdoc_cmd(compiler);
17111711
builder.add_rust_test_threads(&mut cmd);
1712+
// allow for unstable options such as new editions
1713+
cmd.arg("-Z");
1714+
cmd.arg("unstable-options");
17121715
cmd.arg("--test");
17131716
cmd.arg(markdown);
17141717
cmd.env("RUSTC_BOOTSTRAP", "1");

src/test/ui/const-generics/min_const_generics/const-expression-suggest-missing-braces.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn b() {
1313
//~| ERROR expected trait, found constant `BAR`
1414
//~| ERROR type provided when a constant was expected
1515
//~| WARN trait objects without an explicit `dyn` are deprecated
16+
//~| WARN this was previously accepted by the compiler
1617
}
1718
fn c() {
1819
foo::<3 + 3>(); //~ ERROR expressions must be enclosed in braces

0 commit comments

Comments
 (0)