fix: Add trait alias grammar to rust.ungram

This commit is contained in:
Lukas Wirth 2022-11-11 15:25:15 +01:00
parent add85397ae
commit 6674bd898e
6 changed files with 37 additions and 22 deletions

View file

@ -236,11 +236,19 @@ impl TraitData {
.by_key("rustc_skip_array_during_method_dispatch") .by_key("rustc_skip_array_during_method_dispatch")
.exists(); .exists();
let mut collector = let (items, attribute_calls, diagnostics) = match &tr_def.items {
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr)); Some(items) => {
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items); let mut collector = AssocItemCollector::new(
let (items, attribute_calls, diagnostics) = collector.finish(); db,
module_id,
tree_id.file_id(),
ItemContainerId::TraitId(tr),
);
collector.collect(&item_tree, tree_id.tree_id(), items);
collector.finish()
}
None => Default::default(),
};
( (
Arc::new(TraitData { Arc::new(TraitData {
name, name,

View file

@ -666,7 +666,8 @@ pub struct Trait {
pub generic_params: Interned<GenericParams>, pub generic_params: Interned<GenericParams>,
pub is_auto: bool, pub is_auto: bool,
pub is_unsafe: bool, pub is_unsafe: bool,
pub items: Box<[AssocItem]>, /// This is [`None`] if this Trait is a trait alias.
pub items: Option<Box<[AssocItem]>>,
pub ast_id: FileAstId<ast::Trait>, pub ast_id: FileAstId<ast::Trait>,
} }

View file

@ -451,15 +451,7 @@ impl<'a> Ctx<'a> {
.collect() .collect()
}); });
let ast_id = self.source_ast_id_map.ast_id(trait_def); let ast_id = self.source_ast_id_map.ast_id(trait_def);
let res = Trait { let res = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
name,
visibility,
generic_params,
is_auto,
is_unsafe,
items: items.unwrap_or_default(),
ast_id,
};
Some(id(self.data().traits.alloc(res))) Some(id(self.data().traits.alloc(res)))
} }

View file

@ -375,12 +375,21 @@ impl<'a> Printer<'a> {
} }
w!(self, "trait {}", name); w!(self, "trait {}", name);
self.print_generic_params(generic_params); self.print_generic_params(generic_params);
self.print_where_clause_and_opening_brace(generic_params); match items {
self.indented(|this| { Some(items) => {
for item in &**items { self.print_where_clause_and_opening_brace(generic_params);
this.print_mod_item((*item).into()); self.indented(|this| {
for item in &**items {
this.print_mod_item((*item).into());
}
});
} }
}); None => {
w!(self, " = ");
// FIXME: Print the aliased traits
self.print_where_clause_and_opening_brace(generic_params);
}
}
wln!(self, "}}"); wln!(self, "}}");
} }
ModItem::Impl(it) => { ModItem::Impl(it) => {

View file

@ -239,8 +239,11 @@ Static =
Trait = Trait =
Attr* Visibility? Attr* Visibility?
'unsafe'? 'auto'? 'unsafe'? 'auto'?
'trait' Name GenericParamList? (':' TypeBoundList?)? WhereClause? 'trait' Name GenericParamList?
AssocItemList (
(':' TypeBoundList?)? WhereClause? AssocItemList
| '=' TypeBoundList? WhereClause? ';'
)
AssocItemList = AssocItemList =
'{' Attr* AssocItem* '}' '{' Attr* AssocItem* '}'

View file

@ -407,6 +407,8 @@ impl Trait {
pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) } pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) }
pub fn trait_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![trait]) } pub fn trait_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![trait]) }
pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) } pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]