Skip to content

Commit feafda8

Browse files
authored
Rollup merge of rust-lang#88509 - m-ou-se:dyn-no-left-shift-right-shift-just-single-angle-brackets-please-thanks, r=petrochenkov
Don't suggest extra <> in dyn suggestion. Fixes rust-lang#88508
2 parents caca256 + f8beb8f commit feafda8

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
914914
});
915915

916916
if result.is_ok() {
917-
self.maybe_lint_bare_trait(qpath, hir_id);
917+
self.maybe_lint_bare_trait(qpath, hir_id, span);
918918
self.register_wf_obligation(ty.into(), qself.span, traits::WellFormed(None));
919919
}
920920

@@ -927,18 +927,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
927927
)
928928
}
929929

930-
fn maybe_lint_bare_trait(&self, qpath: &QPath<'_>, hir_id: hir::HirId) {
930+
fn maybe_lint_bare_trait(&self, qpath: &QPath<'_>, hir_id: hir::HirId, span: Span) {
931931
if let QPath::TypeRelative(self_ty, _) = qpath {
932932
if let TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
933933
self_ty.kind
934934
{
935935
let msg = "trait objects without an explicit `dyn` are deprecated";
936936
let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(self_ty.span) {
937937
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
938-
(format!("<dyn ({})>", s), Applicability::MachineApplicable)
938+
(format!("dyn ({})", s), Applicability::MachineApplicable)
939939
}
940-
Ok(s) => (format!("<dyn {}>", s), Applicability::MachineApplicable),
941-
Err(_) => ("<dyn <type>>".to_string(), Applicability::HasPlaceholders),
940+
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
941+
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
942+
};
943+
// Wrap in `<..>` if it isn't already.
944+
let sugg = match self.tcx.sess.source_map().span_to_snippet(span) {
945+
Ok(s) if s.starts_with('<') => sugg,
946+
_ => format!("<{}>", sugg),
942947
};
943948
let replace = String::from("use `dyn`");
944949
if self.sess().edition() >= Edition::Edition2021 {

Diff for: src/test/ui/dyn-keyword/dyn-angle-brackets.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// See https://github.com/rust-lang/rust/issues/88508
2+
// run-rustfix
3+
// edition:2018
4+
#![deny(bare_trait_objects)]
5+
#![allow(dead_code)]
6+
#![allow(unused_imports)]
7+
8+
use std::fmt;
9+
10+
#[derive(Debug)]
11+
pub struct Foo;
12+
13+
impl fmt::Display for Foo {
14+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
<dyn fmt::Debug>::fmt(self, f)
16+
//~^ ERROR trait objects without an explicit `dyn` are deprecated
17+
//~| WARNING this is accepted in the current edition
18+
//~| ERROR trait objects without an explicit `dyn` are deprecated
19+
//~| WARNING this is accepted in the current edition
20+
}
21+
}
22+
23+
fn main() {}

Diff for: src/test/ui/dyn-keyword/dyn-angle-brackets.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// See https://github.com/rust-lang/rust/issues/88508
2+
// run-rustfix
3+
// edition:2018
4+
#![deny(bare_trait_objects)]
5+
#![allow(dead_code)]
6+
#![allow(unused_imports)]
7+
8+
use std::fmt;
9+
10+
#[derive(Debug)]
11+
pub struct Foo;
12+
13+
impl fmt::Display for Foo {
14+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
<fmt::Debug>::fmt(self, f)
16+
//~^ ERROR trait objects without an explicit `dyn` are deprecated
17+
//~| WARNING this is accepted in the current edition
18+
//~| ERROR trait objects without an explicit `dyn` are deprecated
19+
//~| WARNING this is accepted in the current edition
20+
}
21+
}
22+
23+
fn main() {}

Diff for: src/test/ui/dyn-keyword/dyn-angle-brackets.stderr

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: trait objects without an explicit `dyn` are deprecated
2+
--> $DIR/dyn-angle-brackets.rs:15:10
3+
|
4+
LL | <fmt::Debug>::fmt(self, f)
5+
| ^^^^^^^^^^ help: use `dyn`: `dyn fmt::Debug`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/dyn-angle-brackets.rs:4:9
9+
|
10+
LL | #![deny(bare_trait_objects)]
11+
| ^^^^^^^^^^^^^^^^^^
12+
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
13+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
14+
15+
error: trait objects without an explicit `dyn` are deprecated
16+
--> $DIR/dyn-angle-brackets.rs:15:10
17+
|
18+
LL | <fmt::Debug>::fmt(self, f)
19+
| ^^^^^^^^^^ help: use `dyn`: `dyn fmt::Debug`
20+
|
21+
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
22+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)