Skip to content

cargo build fails but building macro-expanded file works #81183

Open
@osa1

Description

@osa1

Cargo.toml:

[package]
name = "instrument_test"
version = "0.1.0"
authors = []
edition = "2018"

[dependencies]
tracing = { path = "/home/omer/rust/tracing/tracing" }
tracing-core = { path = "/home/omer/rust/tracing/tracing-core" }

The 'tracing' package is https://github.com/tokio-rs/tracing master branch (fe570af).

main.rs:

use tracing::instrument;

#[instrument]
fn main() {
    //! testing
}

If I build this with current rustc master (4ba1aaf):

$ cargo +stage1 build
...
    Compiling instrument_test v0.1.0 (/home/omer/rust/instrument_test)
error: an inner attribute is not permitted in this context
 --> src/main.rs:4:1
  |
4 | / fn main() {
5 | |     //! testing
6 | | }
  | |_^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

error: aborting due to previous error

error: could not compile `instrument_test`

To learn more, run the command again with --verbose.

However if I expand the macro/attribute using the same rustc using cargo-expand and replace main.rs with the expanded code, it builds fine. Expanded code:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use tracing::instrument;
fn main() {
    //! testing
    {}
    let __tracing_attr_span = {
        use ::tracing::__macro_support::{Callsite as _, Registration};
        static CALLSITE: ::tracing::__macro_support::MacroCallsite = {
            use ::tracing::__macro_support::{MacroCallsite, Registration};
            static META: ::tracing::Metadata<'static> = {
                ::tracing_core::metadata::Metadata::new(
                    "main",
                    "instrument_test",
                    tracing::Level::INFO,
                    Some("src/main.rs"),
                    Some(3u32),
                    Some("instrument_test"),
                    ::tracing_core::field::FieldSet::new(
                        &[],
                        ::tracing_core::callsite::Identifier(&CALLSITE),
                    ),
                    ::tracing::metadata::Kind::SPAN,
                )
            };
            static REG: Registration = Registration::new(&CALLSITE);
            MacroCallsite::new(&META, &REG)
        };
        let mut interest = ::tracing::collect::Interest::never();
        if tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
            && tracing::Level::INFO <= ::tracing::level_filters::LevelFilter::current()
            && {
                interest = CALLSITE.interest();
                !interest.is_never()
            }
            && CALLSITE.is_enabled(interest)
        {
            let meta = CALLSITE.metadata();
            ::tracing::Span::new(meta, &{ meta.fields().value_set(&[]) })
        } else {
            let span = CALLSITE.disabled_span();
            {};
            span
        }
    };
    let __tracing_attr_guard = __tracing_attr_span.enter();
    {}
}

Interestingly cargo expand prints the same error ("an inner attribute is not permitted in this context") but it's still able to expand the code, and expanded code works.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions