Skip to content

Commit cae8680

Browse files
committed
lowering: Omit bare trait lint on macro call sites
This commit implements a hacky fix for detecting when a span is pointing at a macro call site so that bare trait lints are not made incorrectly.
1 parent 44bf6b6 commit cae8680

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

Diff for: src/librustc/hir/lowering.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -5754,13 +5754,21 @@ impl<'a> LoweringContext<'a> {
57545754
}
57555755

57565756
fn maybe_lint_bare_trait(&self, span: Span, id: NodeId, is_global: bool) {
5757-
self.sess.buffer_lint_with_diagnostic(
5758-
builtin::BARE_TRAIT_OBJECTS,
5759-
id,
5760-
span,
5761-
"trait objects without an explicit `dyn` are deprecated",
5762-
builtin::BuiltinLintDiagnostics::BareTraitObject(span, is_global),
5763-
)
5757+
// FIXME(davidtwco): This is a hack to detect macros which produce spans of the
5758+
// call site which do not have a macro backtrace. See #61963.
5759+
let is_macro_callsite = self.sess.source_map()
5760+
.span_to_snippet(span)
5761+
.map(|snippet| snippet.starts_with("#["))
5762+
.unwrap_or(true);
5763+
if !is_macro_callsite {
5764+
self.sess.buffer_lint_with_diagnostic(
5765+
builtin::BARE_TRAIT_OBJECTS,
5766+
id,
5767+
span,
5768+
"trait objects without an explicit `dyn` are deprecated",
5769+
builtin::BuiltinLintDiagnostics::BareTraitObject(span, is_global),
5770+
)
5771+
}
57645772
}
57655773

57665774
fn wrap_in_try_constructor(

Diff for: src/test/ui/suggestions/issue-61963.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub trait Bar { }
1515
pub struct Qux<T>(T);
1616

1717
#[dom_struct]
18-
//~^ ERROR trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
1918
pub struct Foo {
2019
qux: Qux<Qux<Baz>>,
2120
bar: Box<Bar>,

Diff for: src/test/ui/suggestions/issue-61963.stderr

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: trait objects without an explicit `dyn` are deprecated
2-
--> $DIR/issue-61963.rs:21:14
2+
--> $DIR/issue-61963.rs:20:14
33
|
44
LL | bar: Box<Bar>,
55
| ^^^ help: use `dyn`: `dyn Bar`
@@ -10,11 +10,5 @@ note: lint level defined here
1010
LL | #![deny(bare_trait_objects)]
1111
| ^^^^^^^^^^^^^^^^^^
1212

13-
error: trait objects without an explicit `dyn` are deprecated
14-
--> $DIR/issue-61963.rs:17:1
15-
|
16-
LL | #[dom_struct]
17-
| ^^^^^^^^^^^^^ help: use `dyn`: `dyn #[dom_struct]`
18-
19-
error: aborting due to 2 previous errors
13+
error: aborting due to previous error
2014

0 commit comments

Comments
 (0)