Skip to content

Add linting for crate_type attribute values. #11264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn phase_2_configure_and_expand(sess: Session,
let time_passes = sess.time_passes();

sess.building_library.set(session::building_library(sess.opts, &crate));
sess.outputs.set(session::collect_outputs(sess.opts, crate.attrs));
sess.outputs.set(session::collect_outputs(&sess, crate.attrs));

time(time_passes, "gated feature checking", (), |_|
front::feature_gate::check_crate(sess, &crate));
Expand Down
17 changes: 13 additions & 4 deletions src/librustc/driver/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,14 @@ pub fn building_library(options: &options, crate: &ast::Crate) -> bool {
}
}

pub fn collect_outputs(options: &options,
pub fn collect_outputs(session: &Session,
attrs: &[ast::Attribute]) -> ~[OutputStyle] {
// If we're generating a test executable, then ignore all other output
// styles at all other locations
if options.test {
if session.opts.test {
return ~[OutputExecutable];
}
let mut base = options.outputs.clone();
let mut base = session.opts.outputs.clone();
let mut iter = attrs.iter().filter_map(|a| {
if "crate_type" == a.name() {
match a.value_str() {
Expand All @@ -442,7 +442,16 @@ pub fn collect_outputs(options: &options,
Some(n) if "lib" == n => Some(OutputDylib),
Some(n) if "staticlib" == n => Some(OutputStaticlib),
Some(n) if "bin" == n => Some(OutputExecutable),
_ => None
Some(_) => {
session.add_lint(lint::unknown_crate_type, ast::CRATE_NODE_ID,
a.span, ~"invalid `crate_type` value");
None
}
_ => {
session.add_lint(lint::unknown_crate_type, ast::CRATE_NODE_ID,
a.span, ~"`crate_type` requires a value");
None
}
}
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ pub fn run_compiler(args: &[~str], demitter: @diagnostic::Emitter) {
if crate_file_name {
let lm = link::build_link_meta(sess, attrs, &t_outputs.obj_filename,
&mut ::util::sha2::Sha256::new());
let outputs = session::collect_outputs(sopts, attrs);
let outputs = session::collect_outputs(&sess, attrs);
for &style in outputs.iter() {
let fname = link::filename_for_input(&sess, style, &lm,
&t_outputs.out_filename);
Expand Down
8 changes: 8 additions & 0 deletions src/librustc/middle/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub enum lint {
unsafe_block,
attribute_usage,
unknown_features,
unknown_crate_type,

managed_heap_memory,
owned_heap_memory,
Expand Down Expand Up @@ -335,6 +336,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
desc: "unknown features found in create-level #[feature] directives",
default: deny,
}),

("unknown_crate_type",
LintSpec {
lint: unknown_crate_type,
desc: "unknown crate type found in #[crate_type] directive",
default: deny,
}),
];

/*
Expand Down
6 changes: 6 additions & 0 deletions src/test/compile-fail/invalid-crate-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// regression test for issue 11256
#[crate_type="foo"]; //~ ERROR invalid `crate_type` value

fn main() {
return
}
6 changes: 6 additions & 0 deletions src/test/compile-fail/no_crate_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// regresion test for issue 11256
#[crate_type]; //~ ERROR `crate_type` requires a value

fn main() {
return
}