Skip to content

Commit

Permalink
Add linting for crate_type attribute values.
Browse files Browse the repository at this point in the history
This ensures that the `crate_type` attribute always contains a value,
and does not contain an invalid value.
  • Loading branch information
am0d committed Jan 2, 2014
1 parent c34ef5d commit 8965e34
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/librustc/driver/driver.rs
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
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
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
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
@@ -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
@@ -0,0 +1,6 @@
// regresion test for issue 11256
#[crate_type]; //~ ERROR `crate_type` requires a value

fn main() {
return
}

9 comments on commit 8965e34

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at am0d@8965e34

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging am0d/rust/crate_type_lint = 8965e34 into auto

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am0d/rust/crate_type_lint = 8965e34 merged ok, testing candidate = f0839318

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at am0d@8965e34

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging am0d/rust/crate_type_lint = 8965e34 into auto

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am0d/rust/crate_type_lint = 8965e34 merged ok, testing candidate = d3ae3a2

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 8965e34 Jan 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = d3ae3a2

Please sign in to comment.