Skip to content

Tracking issue for proc_macro::Span inspection APIs #54725

@alexcrichton

Description

@alexcrichton
Member

This issue is intended to track a number of unstable APIs which are used to inspect the contents of a Span for information like the file name, byte position, manufacturing new spans, combining them, etc.

This issue tracks the proc_macro_span unstable feature.

Public API

Already stabilized:

impl Span {
    // as `proc_macro_source_text`
    pub fn source_text(&self) -> Option<String>;

    // as `proc_macro_span_location`
    pub fn start(&self) -> Span;
    pub fn end(&self) -> Span;
    pub fn line(&self) -> usize;
    pub fn column(&self) -> usize;
}

impl Group {
    // as `proc_macro_group_span`
    pub fn span_open(&self) -> Span;
    pub fn span_close(&self) -> Span;
}

About to be stabilized:

impl Span {
    // as `proc_macro_span_file`
    pub fn file(&self) -> String; // Mapped file name, for display purposes.
    pub fn local_file(&self) -> Option<PathBuf>; // Real file name as it exists on disk.
}

Close to stabilization, pending some bikeshedding:

impl Span {
    pub fn byte_range(&self) -> Range<usize>;
    // Open question: Do we want to use the old or new Range type?
}

Things that require more discussion:

impl Span {
    pub fn eq(&self, other: &Span) -> bool;
    pub fn join(&self, other: Span) -> Option<Span>;
    pub fn parent(&self) -> Option<Span>;
    pub fn source(&self) -> Span;
}

impl Literal {
    pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span>;
}

History:

Activity

added
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
T-langRelevant to the language team
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
B-unstableBlocker: Implemented in the nightly compiler and unstable.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Oct 1, 2018
softprops

softprops commented on Oct 10, 2018

@softprops

I wanted to shed some pain this is causing with tarpaulin.

Tarpaulin has worked amazingly well for me and my company as a replacement for kcov which historically been a pain to get accurate, reliable and correct results. At the moment tarpaulin stands as the most promising goto option for codecoverage in rust and just feels more like the only first class tooling option.

Having one of those when choosing to invest in a technology is important for many company's adoption story for checking off code quantity checkmarks. When they see that rust doesn't have a reasonable code quality story that works on stable rust, that's can result in a "pass" rather than "I'll take it". There are currently some work arounds for making this work-ish on stable but it feels very much like the story serde was in a year or so ago when you wanted to show all your friends how amazing serde was but then was embarrassed to show what it took to make work on stable because of a macro stabilization blocker.

JakeTherston

JakeTherston commented on Oct 28, 2018

@JakeTherston

With procedural macros having reached a point where they're very useful on stable, I expect many users will find themselves needing access to this information. Would it be reasonable to only stabilize parts of the Span API that are not too risky? Perhaps exposing a function that optionally returns the path of the file where a macro is invoked if such a file exists?

259 remaining items

m-ou-se

m-ou-se commented on Apr 30, 2025

@m-ou-se
Member

Opened #140514 to stabilize Span::file and Span::local_file.

added 2 commits that reference this issue on May 6, 2025
VictorArcium

VictorArcium commented on Jul 19, 2025

@VictorArcium
Contributor

This should be updated now that proc_macro_span_file has been stabilized (in 1.88).

andrewdavidmackenzie

andrewdavidmackenzie commented on Jul 19, 2025

@andrewdavidmackenzie

I hadn't noticed that had been stabalized - so thanks.
I was able to move my project (finally!) to stable.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosB-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-TrackedLibs issues that are tracked on the team's project board.S-tracking-design-concernsStatus: There are blocking design concerns.T-langRelevant to the language teamT-libs-apiRelevant to the library API 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

        Participants

        @wycats@softprops@alexcrichton@Nemo157@udoprog

        Issue actions

          Tracking issue for `proc_macro::Span` inspection APIs Β· Issue #54725 Β· rust-lang/rust