Skip to content

Commit 2656303

Browse files
committed
Auto merge of #13606 - Veykril:trait-alias, r=Veykril
fix: Add trait alias grammar to rust.ungram We already parse them, but the grammar was never updated to reflect that
2 parents d3531e8 + 6674bd8 commit 2656303

File tree

6 files changed

+37
-22
lines changed

6 files changed

+37
-22
lines changed

crates/hir-def/src/data.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,19 @@ impl TraitData {
236236
.by_key("rustc_skip_array_during_method_dispatch")
237237
.exists();
238238

239-
let mut collector =
240-
AssocItemCollector::new(db, module_id, tree_id.file_id(), ItemContainerId::TraitId(tr));
241-
collector.collect(&item_tree, tree_id.tree_id(), &tr_def.items);
242-
let (items, attribute_calls, diagnostics) = collector.finish();
243-
239+
let (items, attribute_calls, diagnostics) = match &tr_def.items {
240+
Some(items) => {
241+
let mut collector = AssocItemCollector::new(
242+
db,
243+
module_id,
244+
tree_id.file_id(),
245+
ItemContainerId::TraitId(tr),
246+
);
247+
collector.collect(&item_tree, tree_id.tree_id(), items);
248+
collector.finish()
249+
}
250+
None => Default::default(),
251+
};
244252
(
245253
Arc::new(TraitData {
246254
name,

crates/hir-def/src/item_tree.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,8 @@ pub struct Trait {
666666
pub generic_params: Interned<GenericParams>,
667667
pub is_auto: bool,
668668
pub is_unsafe: bool,
669-
pub items: Box<[AssocItem]>,
669+
/// This is [`None`] if this Trait is a trait alias.
670+
pub items: Option<Box<[AssocItem]>>,
670671
pub ast_id: FileAstId<ast::Trait>,
671672
}
672673

crates/hir-def/src/item_tree/lower.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,7 @@ impl<'a> Ctx<'a> {
451451
.collect()
452452
});
453453
let ast_id = self.source_ast_id_map.ast_id(trait_def);
454-
let res = Trait {
455-
name,
456-
visibility,
457-
generic_params,
458-
is_auto,
459-
is_unsafe,
460-
items: items.unwrap_or_default(),
461-
ast_id,
462-
};
454+
let res = Trait { name, visibility, generic_params, is_auto, is_unsafe, items, ast_id };
463455
Some(id(self.data().traits.alloc(res)))
464456
}
465457

crates/hir-def/src/item_tree/pretty.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,21 @@ impl<'a> Printer<'a> {
375375
}
376376
w!(self, "trait {}", name);
377377
self.print_generic_params(generic_params);
378-
self.print_where_clause_and_opening_brace(generic_params);
379-
self.indented(|this| {
380-
for item in &**items {
381-
this.print_mod_item((*item).into());
378+
match items {
379+
Some(items) => {
380+
self.print_where_clause_and_opening_brace(generic_params);
381+
self.indented(|this| {
382+
for item in &**items {
383+
this.print_mod_item((*item).into());
384+
}
385+
});
382386
}
383-
});
387+
None => {
388+
w!(self, " = ");
389+
// FIXME: Print the aliased traits
390+
self.print_where_clause_and_opening_brace(generic_params);
391+
}
392+
}
384393
wln!(self, "}}");
385394
}
386395
ModItem::Impl(it) => {

crates/syntax/rust.ungram

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ Static =
239239
Trait =
240240
Attr* Visibility?
241241
'unsafe'? 'auto'?
242-
'trait' Name GenericParamList? (':' TypeBoundList?)? WhereClause?
243-
AssocItemList
242+
'trait' Name GenericParamList?
243+
(
244+
(':' TypeBoundList?)? WhereClause? AssocItemList
245+
| '=' TypeBoundList? WhereClause? ';'
246+
)
244247

245248
AssocItemList =
246249
'{' Attr* AssocItem* '}'

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ impl Trait {
407407
pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) }
408408
pub fn trait_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![trait]) }
409409
pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) }
410+
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
411+
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
410412
}
411413

412414
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)