Add the -Zcrate-attr=foo nightly rustc flag to inject crate attributes
This commit is contained in:
parent
b18b9edf00
commit
71276c6abc
6 changed files with 66 additions and 3 deletions
|
@ -423,6 +423,7 @@ impl_stable_hash_for!(enum ::syntax_pos::FileName {
|
||||||
Anon,
|
Anon,
|
||||||
MacroExpansion,
|
MacroExpansion,
|
||||||
ProcMacroSourceCode,
|
ProcMacroSourceCode,
|
||||||
|
CliCrateAttr,
|
||||||
CfgSpec,
|
CfgSpec,
|
||||||
Custom(s)
|
Custom(s)
|
||||||
});
|
});
|
||||||
|
|
|
@ -1369,6 +1369,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
|
||||||
"don't run LLVM in parallel (while keeping codegen-units and ThinLTO)"),
|
"don't run LLVM in parallel (while keeping codegen-units and ThinLTO)"),
|
||||||
no_leak_check: bool = (false, parse_bool, [UNTRACKED],
|
no_leak_check: bool = (false, parse_bool, [UNTRACKED],
|
||||||
"disables the 'leak check' for subtyping; unsound, but useful for tests"),
|
"disables the 'leak check' for subtyping; unsound, but useful for tests"),
|
||||||
|
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
|
||||||
|
"inject the given attribute in the crate"),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default_lib_output() -> CrateType {
|
pub fn default_lib_output() -> CrateType {
|
||||||
|
|
|
@ -798,7 +798,7 @@ where
|
||||||
pub fn phase_2_configure_and_expand_inner<'a, F>(
|
pub fn phase_2_configure_and_expand_inner<'a, F>(
|
||||||
sess: &'a Session,
|
sess: &'a Session,
|
||||||
cstore: &'a CStore,
|
cstore: &'a CStore,
|
||||||
krate: ast::Crate,
|
mut krate: ast::Crate,
|
||||||
registry: Option<Registry>,
|
registry: Option<Registry>,
|
||||||
crate_name: &str,
|
crate_name: &str,
|
||||||
addl_plugins: Option<Vec<String>>,
|
addl_plugins: Option<Vec<String>>,
|
||||||
|
@ -810,6 +810,10 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(
|
||||||
where
|
where
|
||||||
F: FnOnce(&ast::Crate) -> CompileResult,
|
F: FnOnce(&ast::Crate) -> CompileResult,
|
||||||
{
|
{
|
||||||
|
krate = time(sess, "attributes injection", || {
|
||||||
|
syntax::attr::inject(krate, &sess.parse_sess, &sess.opts.debugging_opts.crate_attr)
|
||||||
|
});
|
||||||
|
|
||||||
let (mut krate, features) = syntax::config::features(
|
let (mut krate, features) = syntax::config::features(
|
||||||
krate,
|
krate,
|
||||||
&sess.parse_sess,
|
&sess.parse_sess,
|
||||||
|
|
|
@ -22,11 +22,11 @@ pub use self::ReprAttr::*;
|
||||||
pub use self::StabilityLevel::*;
|
pub use self::StabilityLevel::*;
|
||||||
|
|
||||||
use ast;
|
use ast;
|
||||||
use ast::{AttrId, Attribute, Name, Ident, Path, PathSegment};
|
use ast::{AttrId, Attribute, AttrStyle, Name, Ident, Path, PathSegment};
|
||||||
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
|
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
|
||||||
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
|
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind, GenericParam};
|
||||||
use codemap::{BytePos, Spanned, respan, dummy_spanned};
|
use codemap::{BytePos, Spanned, respan, dummy_spanned};
|
||||||
use syntax_pos::Span;
|
use syntax_pos::{FileName, Span};
|
||||||
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
use parse::{self, ParseSess, PResult};
|
use parse::{self, ParseSess, PResult};
|
||||||
|
@ -821,3 +821,33 @@ derive_has_attrs! {
|
||||||
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm,
|
Item, Expr, Local, ast::ForeignItem, ast::StructField, ast::ImplItem, ast::TraitItem, ast::Arm,
|
||||||
ast::Field, ast::FieldPat, ast::Variant_
|
ast::Field, ast::FieldPat, ast::Variant_
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -> ast::Crate {
|
||||||
|
for raw_attr in attrs {
|
||||||
|
let mut parser = parse::new_parser_from_source_str(
|
||||||
|
parse_sess,
|
||||||
|
FileName::CliCrateAttr,
|
||||||
|
raw_attr.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let start_span = parser.span;
|
||||||
|
let (path, tokens) = panictry!(parser.parse_path_and_tokens());
|
||||||
|
let end_span = parser.span;
|
||||||
|
if parser.token != token::Eof {
|
||||||
|
parse_sess.span_diagnostic
|
||||||
|
.span_err(start_span.to(end_span), "invalid crate attribute");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
krate.attrs.push(Attribute {
|
||||||
|
id: mk_attr_id(),
|
||||||
|
style: AttrStyle::Inner,
|
||||||
|
path,
|
||||||
|
tokens,
|
||||||
|
is_sugared_doc: false,
|
||||||
|
span: start_span.to(end_span),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
krate
|
||||||
|
}
|
||||||
|
|
|
@ -100,6 +100,8 @@ pub enum FileName {
|
||||||
ProcMacroSourceCode,
|
ProcMacroSourceCode,
|
||||||
/// Strings provided as --cfg [cfgspec] stored in a crate_cfg
|
/// Strings provided as --cfg [cfgspec] stored in a crate_cfg
|
||||||
CfgSpec,
|
CfgSpec,
|
||||||
|
/// Strings provided as crate attributes in the CLI
|
||||||
|
CliCrateAttr,
|
||||||
/// Custom sources for explicit parser calls from plugins and drivers
|
/// Custom sources for explicit parser calls from plugins and drivers
|
||||||
Custom(String),
|
Custom(String),
|
||||||
}
|
}
|
||||||
|
@ -115,6 +117,7 @@ impl std::fmt::Display for FileName {
|
||||||
Anon => write!(fmt, "<anon>"),
|
Anon => write!(fmt, "<anon>"),
|
||||||
ProcMacroSourceCode => write!(fmt, "<proc-macro source code>"),
|
ProcMacroSourceCode => write!(fmt, "<proc-macro source code>"),
|
||||||
CfgSpec => write!(fmt, "cfgspec"),
|
CfgSpec => write!(fmt, "cfgspec"),
|
||||||
|
CliCrateAttr => write!(fmt, "<crate attribute>"),
|
||||||
Custom(ref s) => write!(fmt, "<{}>", s),
|
Custom(ref s) => write!(fmt, "<{}>", s),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -137,6 +140,7 @@ impl FileName {
|
||||||
MacroExpansion |
|
MacroExpansion |
|
||||||
ProcMacroSourceCode |
|
ProcMacroSourceCode |
|
||||||
CfgSpec |
|
CfgSpec |
|
||||||
|
CliCrateAttr |
|
||||||
Custom(_) |
|
Custom(_) |
|
||||||
QuoteExpansion => false,
|
QuoteExpansion => false,
|
||||||
}
|
}
|
||||||
|
@ -150,6 +154,7 @@ impl FileName {
|
||||||
MacroExpansion |
|
MacroExpansion |
|
||||||
ProcMacroSourceCode |
|
ProcMacroSourceCode |
|
||||||
CfgSpec |
|
CfgSpec |
|
||||||
|
CliCrateAttr |
|
||||||
Custom(_) |
|
Custom(_) |
|
||||||
QuoteExpansion => false,
|
QuoteExpansion => false,
|
||||||
Macros(_) => true,
|
Macros(_) => true,
|
||||||
|
|
21
src/test/run-pass/z-crate-attr.rs
Normal file
21
src/test/run-pass/z-crate-attr.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
// This test checks if an unstable feature is enabled with the -Zcrate-attr=feature(foo) flag. If
|
||||||
|
// the exact feature used here is causing problems feel free to replace it with another
|
||||||
|
// perma-unstable feature.
|
||||||
|
|
||||||
|
// compile-flags: -Zcrate-attr=feature(abi_unadjusted)
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
extern "unadjusted" fn foo() {}
|
||||||
|
|
||||||
|
fn main() {}
|
Loading…
Add table
Add a link
Reference in a new issue