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

proc_macro_attribute input has invalid span for various input. #76874

Closed
gui1117 opened this issue Sep 18, 2020 · 4 comments
Closed

proc_macro_attribute input has invalid span for various input. #76874

gui1117 opened this issue Sep 18, 2020 · 4 comments
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gui1117
Copy link
Contributor

gui1117 commented Sep 18, 2020

Context:

input of proc_macro_attribute have invalid (null) span for some input.
I test spans with this code:

#[proc_macro_attribute]
pub fn bar(_attr: proc_macro::TokenStream, item: proc_macro::TokenStream) -> proc_macro::TokenStream {
	for tt in item.clone().into_iter() {
		println!("tt: {}: {:?}", tt, tt.span());
	}
	item
}

Failing inputs:

1- inner doc (this is fixed by #76130 )

#[ressai_proc::bar]
pub mod inner_doc {
	//! With this documentation line removed span are working.
}

prints

tt: pub: #0 bytes(0..0)
tt: mod: #0 bytes(0..0)
tt: inner_doc: #0 bytes(0..0)
tt: {
    //! With this documentation line removed span are working.

}: #0 bytes(0..0)

removing the inner doc makes the token tree having non-null spans.

2- another path attribute (with two path segment) (this one is now fixed in rustc 1.49.0-nightly (ffa2e7a 2020-10-24))

#[ressai_proc::bar]
#[ressai_proc::bar]
pub mod another_attribute {
}

prints

tt: #: #0 bytes(0..0)
tt: [ressai_proc :: bar]: #0 bytes(0..0)
tt: pub: #0 bytes(0..0)
tt: mod: #0 bytes(0..0)
tt: another_attribute: #0 bytes(0..0)
tt: { }: #0 bytes(0..0)
tt: pub: #0 bytes(0..0)
tt: mod: #0 bytes(0..0)
tt: another_attribute: #0 bytes(0..0)
tt: { }: #0 bytes(0..0)

note that this slightly modified input will give non-null spans:

use ressai_proc::bar;
#[ressai_proc::bar]
#[bar]
pub mod another_attribute_but_successful {
}

3 - some method on associated type: (this is fixed by #78980)

#[ressai_proc::bar]
mod foo {
	trait Config {
		type Origin: From<u32>;
	}

	struct A<T>(T);

	impl<T: Config> A<T> {
		fn foo() {
			<T as Config>::Origin::from(3);
		}
	}
}

This will output:

   Compiling ressai-proc v0.1.0 (/home/thiolliere/Developpement/ressai-procedural/ressai-proc)
tt: mod: #0 bytes(0..0)
tt: foo: #0 bytes(0..0)
tt: {
    trait Config { type Origin : From < u32 > ; } struct A < T > (T) ; impl <
    T : Config > A < T > { fn foo() { < T as Config > :: from(3) ; } }
}: #0 bytes(0..0)
error[E0576]: cannot find method or associated constant `from` in trait `Config`

So the rust error message is not pointing the failing code. All span are 0.

Meta

cargo rustc -- --version --verbose:

rustc 1.49.0-nightly (ffa2e7ae8 2020-10-24)
binary: rustc
commit-hash: ffa2e7ae8fbf9badc035740db949b9dae271c29f
commit-date: 2020-10-24
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0

EDIT: related #43081

@gui1117 gui1117 added the C-bug Category: This is a bug. label Sep 18, 2020
@jonas-schievink jonas-schievink added the A-proc-macros Area: Procedural macros label Sep 18, 2020
@Aaron1011
Copy link
Member

Both of these issues will be fixed by #76130

@gui1117
Copy link
Contributor Author

gui1117 commented Nov 5, 2020

update:

  • I added another failing input.
  • the second input is not failing anymore
  • Also I tried to check if [WIP] Token-based outer attributes handling #76130 fix all of them, so I compiled with channel=dev, extended=true and tools=["cargo"] and then used build/x86_64-unknown-linux-gnu/stage1-tools-bin/cargo but the span are still wrong for all input (1 and 3). I guess there should be something wrong in my setup though as Aaron1011 tends to imply otherwise
    sorry my setup was wrong, aaron1011 branch indeed fix the first example. Thus only the 3, latest added one, is still failing with his branch

@gui1117
Copy link
Contributor Author

gui1117 commented Nov 12, 2020

the third input should be fixed by #78980

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 13, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Nov 13, 2020
Fix rustc_ast_pretty print_qpath resulting in invalid macro input

related rust-lang#76874 (third case)

### Issue:

The input for a procedural macro is incorrect, for the rust code:
```rust

mod m {
    pub trait Tr {
        type Ts: super::Tu;
    }
}

trait Tu {
    fn dummy() { }
}

#[may_proc_macro]
fn foo() {
    <T as m::Tr>::Ts::dummy();
}
```
the macro will get the input:
```rust
fn foo() {
    <T as m::Tr>::dummy();
}
```
Thus `Ts` has disappeared.

### Fix:

This is due to invalid pretty print of qpath. This PR fix it.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Nov 13, 2020
Fix rustc_ast_pretty print_qpath resulting in invalid macro input

related rust-lang#76874 (third case)

### Issue:

The input for a procedural macro is incorrect, for the rust code:
```rust

mod m {
    pub trait Tr {
        type Ts: super::Tu;
    }
}

trait Tu {
    fn dummy() { }
}

#[may_proc_macro]
fn foo() {
    <T as m::Tr>::Ts::dummy();
}
```
the macro will get the input:
```rust
fn foo() {
    <T as m::Tr>::dummy();
}
```
Thus `Ts` has disappeared.

### Fix:

This is due to invalid pretty print of qpath. This PR fix it.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Nov 14, 2020
Fix rustc_ast_pretty print_qpath resulting in invalid macro input

related rust-lang#76874 (third case)

### Issue:

The input for a procedural macro is incorrect, for the rust code:
```rust

mod m {
    pub trait Tr {
        type Ts: super::Tu;
    }
}

trait Tu {
    fn dummy() { }
}

#[may_proc_macro]
fn foo() {
    <T as m::Tr>::Ts::dummy();
}
```
the macro will get the input:
```rust
fn foo() {
    <T as m::Tr>::dummy();
}
```
Thus `Ts` has disappeared.

### Fix:

This is due to invalid pretty print of qpath. This PR fix it.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Nov 15, 2020
Fix rustc_ast_pretty print_qpath resulting in invalid macro input

related rust-lang#76874 (third case)

### Issue:

The input for a procedural macro is incorrect, for the rust code:
```rust

mod m {
    pub trait Tr {
        type Ts: super::Tu;
    }
}

trait Tu {
    fn dummy() { }
}

#[may_proc_macro]
fn foo() {
    <T as m::Tr>::Ts::dummy();
}
```
the macro will get the input:
```rust
fn foo() {
    <T as m::Tr>::dummy();
}
```
Thus `Ts` has disappeared.

### Fix:

This is due to invalid pretty print of qpath. This PR fix it.
@gui1117
Copy link
Contributor Author

gui1117 commented Feb 5, 2021

the inner doc issue is remaining but it is a duplicate of #43081

@gui1117 gui1117 closed this as completed Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macros Area: Procedural macros C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants