5632: Cleanup impl gramamr r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-31 18:29:21 +00:00 committed by GitHub
commit 215b9b9ccc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 19 additions and 22 deletions

View file

@ -81,7 +81,7 @@ impl<'a> SubstituteTypeParams<'a> {
// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the // FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
// trait ref, and then go from the types in the substs back to the syntax). // trait ref, and then go from the types in the substs back to the syntax).
fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Vec<ast::Type>> { fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Vec<ast::Type>> {
let target_trait = impl_def.target_trait()?; let target_trait = impl_def.trait_()?;
let path_type = match target_trait { let path_type = match target_trait {
ast::Type::PathType(path) => path, ast::Type::PathType(path) => path,
_ => return None, _ => return None,

View file

@ -111,11 +111,8 @@ pub(crate) fn resolve_target_trait(
sema: &Semantics<RootDatabase>, sema: &Semantics<RootDatabase>,
impl_def: &ast::Impl, impl_def: &ast::Impl,
) -> Option<hir::Trait> { ) -> Option<hir::Trait> {
let ast_path = impl_def let ast_path =
.target_trait() impl_def.trait_().map(|it| it.syntax().clone()).and_then(ast::PathType::cast)?.path()?;
.map(|it| it.syntax().clone())
.and_then(ast::PathType::cast)?
.path()?;
match sema.resolve_path(&ast_path) { match sema.resolve_path(&ast_path) {
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def), Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => Some(def),

View file

@ -448,8 +448,8 @@ impl Ctx {
fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> { fn lower_impl(&mut self, impl_def: &ast::Impl) -> Option<FileItemTreeId<Impl>> {
let generic_params = let generic_params =
self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def); self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def);
let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); let target_trait = impl_def.trait_().map(|tr| self.lower_type_ref(&tr));
let target_type = self.lower_type_ref(&impl_def.target_type()?); let target_type = self.lower_type_ref(&impl_def.self_ty()?);
let is_negative = impl_def.excl_token().is_some(); let is_negative = impl_def.excl_token().is_some();
// We cannot use `assoc_items()` here as that does not include macro calls. // We cannot use `assoc_items()` here as that does not include macro calls.

View file

@ -253,7 +253,7 @@ impl ToNav for hir::ImplDef {
let focus_range = if derive_attr.is_some() { let focus_range = if derive_attr.is_some() {
None None
} else { } else {
src.value.target_type().map(|ty| original_range(db, src.with_value(ty.syntax())).range) src.value.self_ty().map(|ty| original_range(db, src.with_value(ty.syntax())).range)
}; };
NavigationTarget::from_syntax( NavigationTarget::from_syntax(

View file

@ -130,8 +130,8 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
ast::Const(it) => decl_with_type_ref(&it, it.ty()), ast::Const(it) => decl_with_type_ref(&it, it.ty()),
ast::Static(it) => decl_with_type_ref(&it, it.ty()), ast::Static(it) => decl_with_type_ref(&it, it.ty()),
ast::Impl(it) => { ast::Impl(it) => {
let target_type = it.target_type()?; let target_type = it.self_ty()?;
let target_trait = it.target_trait(); let target_trait = it.trait_();
let label = match target_trait { let label = match target_trait {
None => format!("impl {}", target_type.syntax().text()), None => format!("impl {}", target_type.syntax().text()),
Some(t) => { Some(t) => {

View file

@ -194,7 +194,7 @@ fn text_edit_from_self_param(
new_name: &str, new_name: &str,
) -> Option<TextEdit> { ) -> Option<TextEdit> {
fn target_type_name(impl_def: &ast::Impl) -> Option<String> { fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
if let Some(ast::Type::PathType(p)) = impl_def.target_type() { if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
return Some(p.path()?.segment()?.name_ref()?.text().to_string()); return Some(p.path()?.segment()?.name_ref()?.text().to_string());
} }
None None

View file

@ -267,7 +267,6 @@ impl Impl {
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) } pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
pub fn impl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![impl]) } pub fn impl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![impl]) }
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) } pub fn excl_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![!]) }
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) } pub fn assoc_item_list(&self) -> Option<AssocItemList> { support::child(&self.syntax) }

View file

@ -136,14 +136,14 @@ impl ast::UseTreeList {
} }
impl ast::Impl { impl ast::Impl {
pub fn target_type(&self) -> Option<ast::Type> { pub fn self_ty(&self) -> Option<ast::Type> {
match self.target() { match self.target() {
(Some(t), None) | (_, Some(t)) => Some(t), (Some(t), None) | (_, Some(t)) => Some(t),
_ => None, _ => None,
} }
} }
pub fn target_trait(&self) -> Option<ast::Type> { pub fn trait_(&self) -> Option<ast::Type> {
match self.target() { match self.target() {
(Some(t), Some(_)) => Some(t), (Some(t), Some(_)) => Some(t),
_ => None, _ => None,

View file

@ -208,7 +208,7 @@ fn validate_visibility(vis: ast::Visibility, errors: &mut Vec<SyntaxError>) {
Some(it) => it, Some(it) => it,
None => return, None => return,
}; };
if impl_def.target_trait().is_some() { if impl_def.trait_().is_some() {
errors.push(SyntaxError::new("Unnecessary visibility qualifier", vis.syntax.text_range())); errors.push(SyntaxError::new("Unnecessary visibility qualifier", vis.syntax.text_range()));
} }
} }

View file

@ -591,6 +591,8 @@ fn lower_rule(acc: &mut Vec<Field>, grammar: &Grammar, label: Option<&String>, r
| "index" | "index"
| "base" | "base"
| "value" | "value"
| "target_type"
| "target_trait"
); );
if manually_implemented { if manually_implemented {
return; return;

View file

@ -195,10 +195,9 @@ AssocItem =
Impl = Impl =
Attr* Visibility? Attr* Visibility?
'default'? 'unsafe'? 'impl' 'const'? GenericParamList? ( 'default'? 'unsafe'? 'impl' 'const'? GenericParamList?
Type ('!'? target_trait:Type 'for')? target_type:Type
| '!'? Type 'for' Type WhereClause?
) WhereClause?
AssocItemList AssocItemList
ExternBlock = ExternBlock =