Skip to content

Commit 1ccada6

Browse files
committed
Auto merge of #33735 - jseyfried:concat_idents_in_ty_positions, r=nrc
Allow `concat_idents!` in type positions as well as in expression positions This allows the `concat_idents!` macro in type positions as well as in expression positions. r? @nrc
2 parents 4c6b6c2 + e992794 commit 1ccada6

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

src/libsyntax_ext/concat_idents.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,36 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
5252
}
5353
let res = str_to_ident(&res_str);
5454

55-
let e = P(ast::Expr {
56-
id: ast::DUMMY_NODE_ID,
57-
node: ast::ExprKind::Path(None,
58-
ast::Path {
59-
span: sp,
60-
global: false,
61-
segments: vec!(
62-
ast::PathSegment {
63-
identifier: res,
64-
parameters: ast::PathParameters::none(),
65-
}
66-
)
67-
}
68-
),
69-
span: sp,
70-
attrs: None,
71-
});
72-
MacEager::expr(e)
55+
struct Result { ident: ast::Ident, span: Span };
56+
57+
impl Result {
58+
fn path(&self) -> ast::Path {
59+
let segment = ast::PathSegment {
60+
identifier: self.ident,
61+
parameters: ast::PathParameters::none()
62+
};
63+
ast::Path { span: self.span, global: false, segments: vec![segment] }
64+
}
65+
}
66+
67+
impl base::MacResult for Result {
68+
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
69+
Some(P(ast::Expr {
70+
id: ast::DUMMY_NODE_ID,
71+
node: ast::ExprKind::Path(None, self.path()),
72+
span: self.span,
73+
attrs: None,
74+
}))
75+
}
76+
77+
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
78+
Some(P(ast::Ty {
79+
id: ast::DUMMY_NODE_ID,
80+
node: ast::TyKind::Path(None, self.path()),
81+
span: self.span,
82+
}))
83+
}
84+
}
85+
86+
Box::new(Result { ident: res, span: sp })
7387
}

src/test/compile-fail/syntax-extension-minor.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// this now fails (correctly, I claim) because hygiene prevents
12-
// the assembled identifier from being a reference to the binding.
13-
#![feature(concat_idents)]
11+
#![feature(concat_idents, type_macros)]
1412

1513
pub fn main() {
14+
struct Foo;
15+
let _: concat_idents!(F, oo) = Foo; // Test that `concat_idents!` can be used in type positions
16+
1617
let asdf_fdsa = "<.<".to_string();
18+
// this now fails (correctly, I claim) because hygiene prevents
19+
// the assembled identifier from being a reference to the binding.
1720
assert!(concat_idents!(asd, f_f, dsa) == "<.<".to_string());
1821
//~^ ERROR: unresolved name `asdf_fdsa`
1922

0 commit comments

Comments
 (0)