Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate the span of Self keyword to QSelf #103

Merged
merged 1 commit into from Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/receiver.rs
@@ -1,12 +1,12 @@
use crate::respan::respan;
use proc_macro2::{Group, Spacing, Span, TokenStream, TokenTree};
use quote::quote;
use quote::{quote, quote_spanned};
use std::iter::FromIterator;
use std::mem;
use syn::punctuated::Punctuated;
use syn::visit_mut::{self, VisitMut};
use syn::{
parse_quote, Block, Error, ExprPath, ExprStruct, Ident, Item, Macro, PatPath, PatStruct,
parse_quote, token, Block, Error, ExprPath, ExprStruct, Ident, Item, Macro, PatPath, PatStruct,
PatTupleStruct, Path, PathArguments, QSelf, Receiver, Signature, Type, TypePath,
WherePredicate,
};
Expand Down Expand Up @@ -105,12 +105,13 @@ impl ReplaceReceiver {
return;
}

let span = first.ident.span();
*qself = Some(QSelf {
lt_token: Default::default(),
ty: Box::new(self.self_ty(first.ident.span())),
lt_token: token::Lt(span),
ty: Box::new(self.self_ty(span)),
position: 0,
as_token: None,
gt_token: Default::default(),
gt_token: token::Gt(span),
});

if include_as_trait && self.as_trait.is_some() {
Expand Down Expand Up @@ -182,7 +183,8 @@ impl ReplaceReceiver {
let next = iter.next().unwrap();
match iter.peek() {
Some(TokenTree::Punct(p)) if p.as_char() == ':' => {
out.extend(quote!(<#self_ty>))
let span = ident.span();
out.extend(quote_spanned!(span=> <#self_ty>));
}
_ => out.extend(quote!(#self_ty)),
}
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/self-span.rs
Expand Up @@ -2,6 +2,10 @@ use async_trait::async_trait;

pub struct S {}

pub enum E {
V {}
}

#[async_trait]
pub trait Trait {
async fn method(self);
Expand All @@ -15,4 +19,12 @@ impl Trait for S {
}
}

#[async_trait]
impl Trait for E {
async fn method(self) {
let _: () = self;
let _: Self = Self::V;
}
}

fn main() {}
22 changes: 18 additions & 4 deletions tests/ui/self-span.stderr
@@ -1,16 +1,30 @@
error[E0423]: expected value, found struct `S`
--> $DIR/self-span.rs:14:23
--> $DIR/self-span.rs:18:23
|
3 | pub struct S {}
| --------------- `S` defined here
...
14 | let _: Self = Self;
18 | let _: Self = Self;
| ^^^^ did you mean `S { /* fields */ }`?

error[E0308]: mismatched types
--> $DIR/self-span.rs:13:21
--> $DIR/self-span.rs:17:21
|
13 | let _: () = self;
17 | let _: () = self;
| -- ^^^^ expected `()`, found struct `S`
| |
| expected due to this

error[E0308]: mismatched types
--> $DIR/self-span.rs:25:21
|
25 | let _: () = self;
| -- ^^^^ expected `()`, found enum `E`
| |
| expected due to this

error[E0533]: expected unit struct, unit variant or constant, found struct variant `Self::V`
--> $DIR/self-span.rs:26:23
|
26 | let _: Self = Self::V;
| ^^^^^^^