forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#63014 - davidtwco:rustfix-incorrect-dyn-sug…
…gestion, r=estebank Stop bare trait lint applying to macro call sites Fixes rust-lang#61963. Apologies for the delay with in fixing this. If anyone has a better idea how to detect this macro call site case, I'd be happy to fix this in a more robust, less hacky way. r? @estebank
- Loading branch information
Showing
5 changed files
with
134 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{Group, TokenStream, TokenTree}; | ||
|
||
// This macro exists as part of a reproduction of #61963 but without using quote/syn/proc_macro2. | ||
|
||
#[proc_macro_derive(DomObject)] | ||
pub fn expand_token_stream(input: TokenStream) -> TokenStream { | ||
// Construct a dummy span - `#0 bytes(0..0)` - which is present in the input because | ||
// of the specially crafted generated tokens in the `attribute-crate` proc-macro. | ||
let dummy_span = input.clone().into_iter().nth(0).unwrap().span(); | ||
|
||
// Define what the macro would output if constructed properly from the source using syn/quote. | ||
let output: TokenStream = "impl Bar for ((), Qux<Qux<Baz> >) { } | ||
impl Bar for ((), Box<Bar>) { }".parse().unwrap(); | ||
|
||
let mut tokens: Vec<_> = output.into_iter().collect(); | ||
// Adjust token spans to match the original crate (which would use `quote`). Some of the | ||
// generated tokens point to the dummy span. | ||
for token in tokens.iter_mut() { | ||
if let TokenTree::Group(group) = token { | ||
let mut tokens: Vec<_> = group.stream().into_iter().collect(); | ||
for token in tokens.iter_mut().skip(2) { | ||
token.set_span(dummy_span); | ||
} | ||
|
||
let mut stream = TokenStream::new(); | ||
stream.extend(tokens); | ||
*group = Group::new(group.delimiter(), stream); | ||
} | ||
} | ||
|
||
let mut output = TokenStream::new(); | ||
output.extend(tokens); | ||
output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// force-host | ||
// no-prefer-dynamic | ||
#![crate_type = "proc-macro"] | ||
|
||
extern crate proc_macro; | ||
|
||
use proc_macro::{Group, Spacing, Punct, TokenTree, TokenStream}; | ||
|
||
// This macro exists as part of a reproduction of #61963 but without using quote/syn/proc_macro2. | ||
|
||
#[proc_macro_attribute] | ||
pub fn dom_struct(_: TokenStream, input: TokenStream) -> TokenStream { | ||
// Construct the expected output tokens - the input but with a `#[derive(DomObject)]` applied. | ||
let attributes: TokenStream = | ||
"#[derive(DomObject)]".to_string().parse().unwrap(); | ||
let output: TokenStream = attributes.into_iter() | ||
.chain(input.into_iter()).collect(); | ||
|
||
let mut tokens: Vec<_> = output.into_iter().collect(); | ||
// Adjust the spacing of `>` tokens to match what `quote` would produce. | ||
for token in tokens.iter_mut() { | ||
if let TokenTree::Group(group) = token { | ||
let mut tokens: Vec<_> = group.stream().into_iter().collect(); | ||
for token in tokens.iter_mut() { | ||
if let TokenTree::Punct(p) = token { | ||
if p.as_char() == '>' { | ||
*p = Punct::new('>', Spacing::Alone); | ||
} | ||
} | ||
} | ||
|
||
let mut stream = TokenStream::new(); | ||
stream.extend(tokens); | ||
*group = Group::new(group.delimiter(), stream); | ||
} | ||
} | ||
|
||
let mut output = TokenStream::new(); | ||
output.extend(tokens); | ||
output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// aux-build:issue-61963.rs | ||
// aux-build:issue-61963-1.rs | ||
#![deny(bare_trait_objects)] | ||
|
||
#[macro_use] | ||
extern crate issue_61963; | ||
#[macro_use] | ||
extern crate issue_61963_1; | ||
|
||
// This test checks that the bare trait object lint does not trigger on macro attributes that | ||
// generate code which would trigger the lint. | ||
|
||
pub struct Baz; | ||
pub trait Bar { } | ||
pub struct Qux<T>(T); | ||
|
||
#[dom_struct] | ||
pub struct Foo { | ||
qux: Qux<Qux<Baz>>, | ||
bar: Box<Bar>, | ||
//~^ ERROR trait objects without an explicit `dyn` are deprecated [bare_trait_objects] | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: trait objects without an explicit `dyn` are deprecated | ||
--> $DIR/issue-61963.rs:20:14 | ||
| | ||
LL | bar: Box<Bar>, | ||
| ^^^ help: use `dyn`: `dyn Bar` | ||
| | ||
note: lint level defined here | ||
--> $DIR/issue-61963.rs:3:9 | ||
| | ||
LL | #![deny(bare_trait_objects)] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|