Skip to content

proc_macro_attribute input has invalid span for various input. #76874

Closed

Description

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions