Track the outer type's spans too for implicit impls

This commit is contained in:
David Tolnay 2021-03-27 01:46:01 -04:00
parent eb55cee41e
commit 9dab9d8e38
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -1,5 +1,5 @@
use crate::syntax::{NamedType, Type};
use proc_macro2::Ident;
use crate::syntax::{NamedType, Ty1, Type};
use proc_macro2::{Ident, Span};
use std::hash::{Hash, Hasher};
use syn::Token;
@ -15,36 +15,38 @@ pub enum ImplKey<'a> {
#[derive(Copy, Clone)]
pub struct NamedImplKey<'a> {
pub begin_span: Span,
pub rust: &'a Ident,
pub lt_token: Option<Token![<]>,
pub gt_token: Option<Token![>]>,
pub end_span: Span,
}
impl Type {
pub(crate) fn impl_key(&self) -> Option<ImplKey> {
if let Type::RustBox(ty) = self {
if let Type::Ident(ident) = &ty.inner {
return Some(ImplKey::RustBox(NamedImplKey::from(ident)));
return Some(ImplKey::RustBox(NamedImplKey::new(ty, ident)));
}
} else if let Type::RustVec(ty) = self {
if let Type::Ident(ident) = &ty.inner {
return Some(ImplKey::RustVec(NamedImplKey::from(ident)));
return Some(ImplKey::RustVec(NamedImplKey::new(ty, ident)));
}
} else if let Type::UniquePtr(ty) = self {
if let Type::Ident(ident) = &ty.inner {
return Some(ImplKey::UniquePtr(NamedImplKey::from(ident)));
return Some(ImplKey::UniquePtr(NamedImplKey::new(ty, ident)));
}
} else if let Type::SharedPtr(ty) = self {
if let Type::Ident(ident) = &ty.inner {
return Some(ImplKey::SharedPtr(NamedImplKey::from(ident)));
return Some(ImplKey::SharedPtr(NamedImplKey::new(ty, ident)));
}
} else if let Type::WeakPtr(ty) = self {
if let Type::Ident(ident) = &ty.inner {
return Some(ImplKey::WeakPtr(NamedImplKey::from(ident)));
return Some(ImplKey::WeakPtr(NamedImplKey::new(ty, ident)));
}
} else if let Type::CxxVector(ty) = self {
if let Type::Ident(ident) = &ty.inner {
return Some(ImplKey::CxxVector(NamedImplKey::from(ident)));
return Some(ImplKey::CxxVector(NamedImplKey::new(ty, ident)));
}
}
None
@ -65,12 +67,14 @@ impl<'a> Hash for NamedImplKey<'a> {
}
}
impl<'a> From<&'a NamedType> for NamedImplKey<'a> {
fn from(ty: &'a NamedType) -> Self {
impl<'a> NamedImplKey<'a> {
fn new(outer: &Ty1, inner: &'a NamedType) -> Self {
NamedImplKey {
rust: &ty.rust,
lt_token: ty.generics.lt_token,
gt_token: ty.generics.gt_token,
begin_span: outer.name.span(),
rust: &inner.rust,
lt_token: inner.generics.lt_token,
gt_token: inner.generics.gt_token,
end_span: outer.rangle.span,
}
}
}