Skip to content

Commit 528366d

Browse files
committed
Revise and generalize the macros-unlinted tests.
Review feedback asked for the test to be generalized to include macros 2.0; that generalization is dyn-2015-idents-in-decl-macros-unlinted.rs As a drive-by, I also decided to revise the test to make it clear *why* we cannot generally lint these cases. (I already had similar demonstrations in dyn-2015-edition-keyword-ident-lint.rs, but it does not hurt to try to emphasize matters.) I also added some commentary on the cases where we could choose to make the lint smarter, namely the situations where a macro is *definitely* using `dyn` as an identifier (because it is using it as a path component).
1 parent f043d2d commit 528366d

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// compile-pass
2+
3+
// Under the 2015 edition with the keyword_idents lint, `dyn` is
4+
// not entirely acceptable as an identifier.
5+
//
6+
// We currently do not attempt to detect or fix uses of `dyn` as an
7+
// identifier under a macro, including under the declarative `macro`
8+
// forms from macros 1.2 and macros 2.0.
9+
10+
#![feature(decl_macro)]
11+
#![allow(non_camel_case_types)]
12+
#![deny(keyword_idents)]
13+
14+
mod outer_mod {
15+
pub mod r#dyn {
16+
pub struct r#dyn;
17+
}
18+
}
19+
20+
// Here we are illustrating that the current lint does not flag the
21+
// occurrences of `dyn` in this macro definition; however, it
22+
// certainly *could* (and it would be nice if it did), since these
23+
// occurrences are not compatible with the 2018 edition's
24+
// interpretation of `dyn` as a keyword.
25+
macro defn_has_dyn_idents() { ::outer_mod::dyn::dyn }
26+
27+
struct X;
28+
trait Trait { fn hello(&self) { }}
29+
impl Trait for X { }
30+
31+
macro tt_trait($arg:tt) { & $arg Trait }
32+
macro id_trait($id:ident) { & $id Trait }
33+
34+
fn main() {
35+
defn_has_dyn_idents!();
36+
37+
// Here we are illustrating that the current lint does not flag
38+
// the occurrences of `dyn` in these macro invocations. It
39+
// definitely should *not* flag the one in `tt_trait`, since that
40+
// is expanding in a valid fashion to `&dyn Trait`.
41+
//
42+
// It is arguable whether it would be valid to flag the occurrence
43+
// in `id_trait`, since that macro specifies that it takes an
44+
// `ident` as its input.
45+
fn f_tt(x: &X) -> tt_trait!(dyn) { x }
46+
fn f_id(x: &X) -> id_trait!(dyn) { x }
47+
48+
let x = X;
49+
f_tt(&x).hello();
50+
f_id(&x).hello();
51+
}

src/test/ui/dyn-keyword/dyn-2015-idents-in-macros-unlinted.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,42 @@ mod outer_mod {
1515
}
1616
}
1717

18+
// Here we are illustrating that the current lint does not flag the
19+
// occurrences of `dyn` in this macro definition; however, it
20+
// certainly *could* (and it would be nice if it did), since these
21+
// occurrences are not compatible with the 2018 edition's
22+
// interpretation of `dyn` as a keyword.
1823
macro_rules! defn_has_dyn_idents {
19-
($arg:ident) => { ::outer_mod::dyn::dyn }
24+
() => { ::outer_mod::dyn::dyn }
25+
}
26+
27+
struct X;
28+
trait Trait { fn hello(&self) { }}
29+
impl Trait for X { }
30+
31+
macro_rules! tt_trait {
32+
($arg:tt) => { & $arg Trait }
33+
}
34+
35+
macro_rules! id_trait {
36+
($id:ident) => { & $id Trait }
2037
}
2138

2239
fn main() {
23-
defn_has_dyn_idents!(dyn);
40+
defn_has_dyn_idents!();
41+
42+
// Here we are illustrating that the current lint does not flag
43+
// the occurrences of `dyn` in these macro invocations. It
44+
// definitely should *not* flag the one in `tt_trait`, since that
45+
// is expanding in a valid fashion to `&dyn Trait`.
46+
//
47+
// It is arguable whether it would be valid to flag the occurrence
48+
// in `id_trait`, since that macro specifies that it takes an
49+
// `ident` as its input.
50+
fn f_tt(x: &X) -> tt_trait!(dyn) { x }
51+
fn f_id(x: &X) -> id_trait!(dyn) { x }
52+
53+
let x = X;
54+
f_tt(&x).hello();
55+
f_id(&x).hello();
2456
}

0 commit comments

Comments
 (0)