SyntaxContext in rustc_span::hygiene - Rust (original) (raw)

Struct SyntaxContext

Source

pub struct SyntaxContext(u32);

Expand description

Source§

Source

Source

Source

Source

Source

Source

Source

Extend a syntax context with a given expansion and transparency.

Source

Pulls a single mark off of the syntax context. This effectively moves the context up one macro definition level. That is, if we have a nested macro definition as follows:

macro_rules! f {
   macro_rules! g {
       ...
   }
}

and we have a SyntaxContext that is referring to something declared by an invocation of g (call it g1), calling remove_mark will result in the SyntaxContext for the invocation of f that created g1. Returns the mark that was removed.

Source

Source

Adjust this context for resolution in a scope created by the given expansion. For example, consider the following three resolutions of f:

#![feature(decl_macro)]
mod foo {
    pub fn f() {} // `f`'s `SyntaxContext` is empty.
}
m!(f);
macro m($f:ident) {
    mod bar {
        pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
        pub fn <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>f</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mrow></mrow><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">‘</mi></mrow><annotation encoding="application/x-tex">f() {} // `</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mclose">)</span><span class="mord"></span><span class="mord">//‘</span></span></span></span>f`'s `SyntaxContext` is empty.
    }
    foo::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
    //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
    //| and it resolves to `::foo::f`.
    bar::f(); // `f`'s `SyntaxContext` has a single `ExpnId` from `m`
    //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
    //| and it resolves to `::bar::f`.
    bar::$f(); // `f`'s `SyntaxContext` is empty.
    //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
    //| and it resolves to `::bar::$f`.
}

This returns the expansion whose definition scope we use to privacy check the resolution, or None if we privacy check as usual (i.e., not w.r.t. a macro definition scope).

Source

Like SyntaxContext::adjust, but also normalizes self to macros 2.0.

Source

Adjust this context for resolution in a scope created by the given expansion via a glob import with the given SyntaxContext. For example:

#![feature(decl_macro)]
m!(f);
macro m($i:ident) {
    mod foo {
        pub fn f() {} // `f`'s `SyntaxContext` has a single `ExpnId` from `m`.
        pub fn <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mrow></mrow><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">‘</mi></mrow><annotation encoding="application/x-tex">i() {} // `</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">i</span><span class="mopen">(</span><span class="mclose">)</span><span class="mord"></span><span class="mord">//‘</span></span></span></span>i`'s `SyntaxContext` is empty.
    }
    n!(f);
    macro n($j:ident) {
        use foo::*;
        f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
        //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">‘</mi></mrow><annotation encoding="application/x-tex">i(); // `</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">i</span><span class="mopen">(</span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//‘</span></span></span></span>i`'s `SyntaxContext` has a mark from `n`
        //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">‘</mi></mrow><annotation encoding="application/x-tex">j(); // `</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mopen">(</span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//‘</span></span></span></span>j`'s `SyntaxContext` has a mark from `m`
        //^ This cannot be glob-adjusted, so this is a resolution error.
    }
}

This returns None if the context cannot be glob-adjusted. Otherwise, it returns the scope to use when privacy checking (see adjust for details).

Source

Undo glob_adjust if possible:

if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
    assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
}

Source

Source

Source

Source

Source

ctxt.outer_expn_data() is equivalent to but faster thanctxt.outer_expn().expn_data().

Source

Source

Source

Source

Returns whether this context originates in a foreign crate’s external macro.

This is used to test whether a lint should not even begin to figure out whether it should be reported on the current node.

§

§

§

§

§

§

§

§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 4 bytes