Give path segment type anchors their own grammar rule

This commit is contained in:
Lukas Wirth 2025-06-04 11:30:43 +02:00
parent d2164fe08b
commit 7c3de05e3a
14 changed files with 223 additions and 158 deletions

View File

@ -931,6 +931,7 @@ pub fn new() {
// PATH_TYPE@23..26
// PATH@23..26
// PATH_SEGMENT@23..26
// TYPE_ANCHOR@23..26
// L_ANGLE@23..24 "<"
// PAREN_TYPE@24..26
// L_PAREN@24..25 "("

View File

@ -89,7 +89,9 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) -> Option<Completed
// test qual_paths
// type X = <A as B>::Output;
// fn foo() { <usize as Default>::default(); }
if first && p.eat(T![<]) {
if first && p.at(T![<]) {
let m = p.start();
p.bump(T![<]);
// test_err angled_path_without_qual
// type X = <()>;
// type Y = <A as B>;
@ -102,6 +104,7 @@ fn path_segment(p: &mut Parser<'_>, mode: Mode, first: bool) -> Option<Completed
}
}
p.expect(T![>]);
m.complete(p, TYPE_ANCHOR);
if !p.at(T![::]) {
p.error("expected `::`");
}

View File

@ -291,6 +291,7 @@ pub enum SyntaxKind {
TUPLE_STRUCT_PAT,
TUPLE_TYPE,
TYPE_ALIAS,
TYPE_ANCHOR,
TYPE_ARG,
TYPE_BOUND,
TYPE_BOUND_LIST,
@ -463,6 +464,7 @@ impl SyntaxKind {
| TUPLE_STRUCT_PAT
| TUPLE_TYPE
| TYPE_ALIAS
| TYPE_ANCHOR
| TYPE_ARG
| TYPE_BOUND
| TYPE_BOUND_LIST

View File

@ -10,6 +10,7 @@ SOURCE_FILE
PATH_TYPE
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
TUPLE_TYPE
L_PAREN "("
@ -28,6 +29,7 @@ SOURCE_FILE
PATH_TYPE
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH

View File

@ -88,6 +88,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH
@ -119,6 +120,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH

View File

@ -11,6 +11,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH
@ -51,6 +52,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH

View File

@ -19,6 +19,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
INFER_TYPE
UNDERSCORE "_"

View File

@ -84,6 +84,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH

View File

@ -45,6 +45,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH

View File

@ -107,6 +107,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
PATH_TYPE
PATH

View File

@ -288,6 +288,7 @@ SOURCE_FILE
PATH
PATH
PATH_SEGMENT
TYPE_ANCHOR
L_ANGLE "<"
REF_TYPE
AMP "&"

View File

@ -39,7 +39,10 @@ PathSegment =
| NameRef GenericArgList?
| NameRef ParenthesizedArgList RetType?
| NameRef ReturnTypeSyntax
| '<' Type ('as' PathType)? '>'
| TypeAnchor
TypeAnchor =
'<' Type ('as' PathType)? '>'
ReturnTypeSyntax =
'(' '..' ')'

View File

@ -1232,21 +1232,13 @@ impl PathSegment {
support::child(&self.syntax)
}
#[inline]
pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
#[inline]
pub fn ret_type(&self) -> Option<RetType> { support::child(&self.syntax) }
#[inline]
pub fn return_type_syntax(&self) -> Option<ReturnTypeSyntax> { support::child(&self.syntax) }
#[inline]
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
pub fn type_anchor(&self) -> Option<TypeAnchor> { support::child(&self.syntax) }
#[inline]
pub fn coloncolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![::]) }
#[inline]
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
#[inline]
pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
#[inline]
pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
}
pub struct PathType {
pub(crate) syntax: SyntaxNode,
@ -1739,6 +1731,21 @@ impl TypeAlias {
#[inline]
pub fn type_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![type]) }
}
pub struct TypeAnchor {
pub(crate) syntax: SyntaxNode,
}
impl TypeAnchor {
#[inline]
pub fn path_type(&self) -> Option<PathType> { support::child(&self.syntax) }
#[inline]
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
#[inline]
pub fn l_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![<]) }
#[inline]
pub fn r_angle_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![>]) }
#[inline]
pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
}
pub struct TypeArg {
pub(crate) syntax: SyntaxNode,
}
@ -7108,6 +7115,42 @@ impl fmt::Debug for TypeAlias {
f.debug_struct("TypeAlias").field("syntax", &self.syntax).finish()
}
}
impl AstNode for TypeAnchor {
#[inline]
fn kind() -> SyntaxKind
where
Self: Sized,
{
TYPE_ANCHOR
}
#[inline]
fn can_cast(kind: SyntaxKind) -> bool { kind == TYPE_ANCHOR }
#[inline]
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) {
Some(Self { syntax })
} else {
None
}
}
#[inline]
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl hash::Hash for TypeAnchor {
fn hash<H: hash::Hasher>(&self, state: &mut H) { self.syntax.hash(state); }
}
impl Eq for TypeAnchor {}
impl PartialEq for TypeAnchor {
fn eq(&self, other: &Self) -> bool { self.syntax == other.syntax }
}
impl Clone for TypeAnchor {
fn clone(&self) -> Self { Self { syntax: self.syntax.clone() } }
}
impl fmt::Debug for TypeAnchor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TypeAnchor").field("syntax", &self.syntax).finish()
}
}
impl AstNode for TypeArg {
#[inline]
fn kind() -> SyntaxKind
@ -10624,6 +10667,11 @@ impl std::fmt::Display for TypeAlias {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for TypeAnchor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)
}
}
impl std::fmt::Display for TypeArg {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self.syntax(), f)

View File

@ -276,18 +276,15 @@ impl ast::PathSegment {
_ => PathSegmentKind::Name(name_ref),
}
} else {
match self.syntax().first_child_or_token()?.kind() {
T![<] => {
let anchor = self.type_anchor()?;
// FIXME: Move this over to `ast::TypeAnchor`
// <T> or <T as Trait>
// T is any TypeRef, Trait has to be a PathType
let mut type_refs =
self.syntax().children().filter(|node| ast::Type::can_cast(node.kind()));
anchor.syntax().children().filter(|node| ast::Type::can_cast(node.kind()));
let type_ref = type_refs.next().and_then(ast::Type::cast);
let trait_ref = type_refs.next().and_then(ast::PathType::cast);
PathSegmentKind::Type { type_ref, trait_ref }
}
_ => return None,
}
};
Some(res)
}
@ -473,7 +470,7 @@ impl ast::Impl {
// [#15778](https://github.com/rust-lang/rust-analyzer/issues/15778)
impl ast::PathSegment {
pub fn qualifying_trait(&self) -> Option<ast::PathType> {
let mut path_types = support::children(self.syntax());
let mut path_types = support::children(self.type_anchor()?.syntax());
let first = path_types.next()?;
path_types.next().or(Some(first))
}