Skip to content

Commit c72d3a7

Browse files
Merge #7324
7324: Add `MacroType` r=jonas-schievink a=jonas-schievink Adds syntax-level support for macros in type position. Based on #7291 due to the ungrammar update Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents 9daba96 + 872bf09 commit c72d3a7

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_def/src/type_ref.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ impl TypeRef {
159159
ast::Type::DynTraitType(inner) => {
160160
TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
161161
}
162+
// FIXME: Macros in type position are not yet supported.
163+
ast::Type::MacroType(_) => TypeRef::Error,
162164
}
163165
}
164166

crates/parser/src/syntax_kind/generated.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub enum SyntaxKind {
143143
MACRO_DEF,
144144
PAREN_TYPE,
145145
TUPLE_TYPE,
146+
MACRO_TYPE,
146147
NEVER_TYPE,
147148
PATH_TYPE,
148149
PTR_TYPE,

crates/syntax/src/ast/generated/nodes.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,13 @@ impl InferType {
10721072
pub fn underscore_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![_]) }
10731073
}
10741074
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1075+
pub struct MacroType {
1076+
pub(crate) syntax: SyntaxNode,
1077+
}
1078+
impl MacroType {
1079+
pub fn macro_call(&self) -> Option<MacroCall> { support::child(&self.syntax) }
1080+
}
1081+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
10751082
pub struct NeverType {
10761083
pub(crate) syntax: SyntaxNode,
10771084
}
@@ -1300,6 +1307,7 @@ pub enum Type {
13001307
ForType(ForType),
13011308
ImplTraitType(ImplTraitType),
13021309
InferType(InferType),
1310+
MacroType(MacroType),
13031311
NeverType(NeverType),
13041312
ParenType(ParenType),
13051313
PathType(PathType),
@@ -2558,6 +2566,17 @@ impl AstNode for InferType {
25582566
}
25592567
fn syntax(&self) -> &SyntaxNode { &self.syntax }
25602568
}
2569+
impl AstNode for MacroType {
2570+
fn can_cast(kind: SyntaxKind) -> bool { kind == MACRO_TYPE }
2571+
fn cast(syntax: SyntaxNode) -> Option<Self> {
2572+
if Self::can_cast(syntax.kind()) {
2573+
Some(Self { syntax })
2574+
} else {
2575+
None
2576+
}
2577+
}
2578+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
2579+
}
25612580
impl AstNode for NeverType {
25622581
fn can_cast(kind: SyntaxKind) -> bool { kind == NEVER_TYPE }
25632582
fn cast(syntax: SyntaxNode) -> Option<Self> {
@@ -2889,6 +2908,9 @@ impl From<ImplTraitType> for Type {
28892908
impl From<InferType> for Type {
28902909
fn from(node: InferType) -> Type { Type::InferType(node) }
28912910
}
2911+
impl From<MacroType> for Type {
2912+
fn from(node: MacroType) -> Type { Type::MacroType(node) }
2913+
}
28922914
impl From<NeverType> for Type {
28932915
fn from(node: NeverType) -> Type { Type::NeverType(node) }
28942916
}
@@ -2914,8 +2936,8 @@ impl AstNode for Type {
29142936
fn can_cast(kind: SyntaxKind) -> bool {
29152937
match kind {
29162938
ARRAY_TYPE | DYN_TRAIT_TYPE | FN_PTR_TYPE | FOR_TYPE | IMPL_TRAIT_TYPE | INFER_TYPE
2917-
| NEVER_TYPE | PAREN_TYPE | PATH_TYPE | PTR_TYPE | REF_TYPE | SLICE_TYPE
2918-
| TUPLE_TYPE => true,
2939+
| MACRO_TYPE | NEVER_TYPE | PAREN_TYPE | PATH_TYPE | PTR_TYPE | REF_TYPE
2940+
| SLICE_TYPE | TUPLE_TYPE => true,
29192941
_ => false,
29202942
}
29212943
}
@@ -2927,6 +2949,7 @@ impl AstNode for Type {
29272949
FOR_TYPE => Type::ForType(ForType { syntax }),
29282950
IMPL_TRAIT_TYPE => Type::ImplTraitType(ImplTraitType { syntax }),
29292951
INFER_TYPE => Type::InferType(InferType { syntax }),
2952+
MACRO_TYPE => Type::MacroType(MacroType { syntax }),
29302953
NEVER_TYPE => Type::NeverType(NeverType { syntax }),
29312954
PAREN_TYPE => Type::ParenType(ParenType { syntax }),
29322955
PATH_TYPE => Type::PathType(PathType { syntax }),
@@ -2946,6 +2969,7 @@ impl AstNode for Type {
29462969
Type::ForType(it) => &it.syntax,
29472970
Type::ImplTraitType(it) => &it.syntax,
29482971
Type::InferType(it) => &it.syntax,
2972+
Type::MacroType(it) => &it.syntax,
29492973
Type::NeverType(it) => &it.syntax,
29502974
Type::ParenType(it) => &it.syntax,
29512975
Type::PathType(it) => &it.syntax,
@@ -4082,6 +4106,11 @@ impl std::fmt::Display for InferType {
40824106
std::fmt::Display::fmt(self.syntax(), f)
40834107
}
40844108
}
4109+
impl std::fmt::Display for MacroType {
4110+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4111+
std::fmt::Display::fmt(self.syntax(), f)
4112+
}
4113+
}
40854114
impl std::fmt::Display for NeverType {
40864115
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40874116
std::fmt::Display::fmt(self.syntax(), f)

xtask/src/ast_src.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
104104
"MACRO_DEF",
105105
"PAREN_TYPE",
106106
"TUPLE_TYPE",
107+
"MACRO_TYPE",
107108
"NEVER_TYPE",
108109
"PATH_TYPE",
109110
"PTR_TYPE",

0 commit comments

Comments
 (0)