Skip to content

Commit 75f058d

Browse files
committed
Check for macros in built-in attributes that don't support them.
1 parent 5f8c571 commit 75f058d

14 files changed

+142
-8
lines changed

Diff for: compiler/rustc_expand/src/module.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_ast::ptr::P;
33
use rustc_ast::{token, Attribute, Inline, Item};
44
use rustc_errors::{struct_span_err, DiagnosticBuilder};
55
use rustc_parse::new_parser_from_file;
6+
use rustc_parse::validate_attr;
67
use rustc_session::parse::ParseSess;
78
use rustc_session::Session;
89
use rustc_span::symbol::{sym, Ident};
@@ -168,7 +169,25 @@ fn mod_file_path_from_attr(
168169
dir_path: &Path,
169170
) -> Option<PathBuf> {
170171
// Extract path string from first `#[path = "path_string"]` attribute.
171-
let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?.as_str();
172+
let first_path = attrs.iter().find(|at| at.has_name(sym::path))?;
173+
let path_string = match first_path.value_str() {
174+
Some(s) => s.as_str(),
175+
None => {
176+
// This check is here mainly to catch attempting to use a macro,
177+
// such as #[path = concat!(...)]. This isn't currently supported
178+
// because otherwise the InvocationCollector would need to defer
179+
// loading a module until the #[path] attribute was expanded, and
180+
// it doesn't support that (and would likely add a bit of
181+
// complexity). Usually bad forms are checked in AstValidator (via
182+
// `check_builtin_attribute`), but by the time that runs the macro
183+
// is expanded, and it doesn't give an error.
184+
validate_attr::emit_fatal_malformed_builtin_attribute(
185+
&sess.parse_sess,
186+
first_path,
187+
sym::path,
188+
);
189+
}
190+
};
172191

173192
// On windows, the base path might have the form
174193
// `\\?\foo\bar` in which case it does not tolerate

Diff for: compiler/rustc_interface/src/passes.rs

+25-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::middle::cstore::{MetadataLoader, MetadataLoaderDyn};
2323
use rustc_middle::ty::query::Providers;
2424
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
2525
use rustc_mir_build as mir_build;
26-
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str};
26+
use rustc_parse::{parse_crate_from_file, parse_crate_from_source_str, validate_attr};
2727
use rustc_passes::{self, hir_stats, layout_test};
2828
use rustc_plugin_impl as plugin;
2929
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
@@ -33,8 +33,8 @@ use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType, PpMod
3333
use rustc_session::lint;
3434
use rustc_session::output::{filename_for_input, filename_for_metadata};
3535
use rustc_session::search_paths::PathKind;
36-
use rustc_session::Session;
37-
use rustc_span::symbol::{Ident, Symbol};
36+
use rustc_session::{Limit, Session};
37+
use rustc_span::symbol::{sym, Ident, Symbol};
3838
use rustc_span::FileName;
3939
use rustc_trait_selection::traits;
4040
use rustc_typeck as typeck;
@@ -311,8 +311,7 @@ pub fn configure_and_expand(
311311

312312
// Create the config for macro expansion
313313
let features = sess.features_untracked();
314-
let recursion_limit =
315-
rustc_middle::middle::limits::get_recursion_limit(&krate.attrs, &sess);
314+
let recursion_limit = get_recursion_limit(&krate.attrs, &sess);
316315
let cfg = rustc_expand::expand::ExpansionConfig {
317316
features: Some(&features),
318317
recursion_limit,
@@ -1070,3 +1069,24 @@ pub fn start_codegen<'tcx>(
10701069

10711070
codegen
10721071
}
1072+
1073+
fn get_recursion_limit(krate_attrs: &[ast::Attribute], sess: &Session) -> Limit {
1074+
if let Some(attr) = krate_attrs
1075+
.iter()
1076+
.find(|attr| attr.has_name(sym::recursion_limit) && attr.value_str().is_none())
1077+
{
1078+
// This is here mainly to check for using a macro, such as
1079+
// #![recursion_limit = foo!()]. That is not supported since that
1080+
// would require expanding this while in the middle of expansion,
1081+
// which needs to know the limit before expanding. Otherwise,
1082+
// validation would normally be caught in AstValidator (via
1083+
// `check_builtin_attribute`), but by the time that runs the macro
1084+
// is expanded, and it doesn't give an error.
1085+
validate_attr::emit_fatal_malformed_builtin_attribute(
1086+
&sess.parse_sess,
1087+
attr,
1088+
sym::recursion_limit,
1089+
);
1090+
}
1091+
rustc_middle::middle::limits::get_recursion_limit(krate_attrs, sess)
1092+
}

Diff for: compiler/rustc_interface/src/util.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_errors::registry::Registry;
1010
use rustc_metadata::dynamic_lib::DynamicLibrary;
1111
#[cfg(parallel_compiler)]
1212
use rustc_middle::ty::tls;
13+
use rustc_parse::validate_attr;
1314
#[cfg(parallel_compiler)]
1415
use rustc_query_impl::QueryCtxt;
1516
use rustc_resolve::{self, Resolver};
@@ -475,7 +476,7 @@ pub fn get_codegen_sysroot(
475476
}
476477

477478
pub(crate) fn check_attr_crate_type(
478-
_sess: &Session,
479+
sess: &Session,
479480
attrs: &[ast::Attribute],
480481
lint_buffer: &mut LintBuffer,
481482
) {
@@ -515,6 +516,19 @@ pub(crate) fn check_attr_crate_type(
515516
);
516517
}
517518
}
519+
} else {
520+
// This is here mainly to check for using a macro, such as
521+
// #![crate_type = foo!()]. That is not supported since the
522+
// crate type needs to be known very early in compilation long
523+
// before expansion. Otherwise, validation would normally be
524+
// caught in AstValidator (via `check_builtin_attribute`), but
525+
// by the time that runs the macro is expanded, and it doesn't
526+
// give an error.
527+
validate_attr::emit_fatal_malformed_builtin_attribute(
528+
&sess.parse_sess,
529+
a,
530+
sym::crate_type,
531+
);
518532
}
519533
}
520534
}

Diff for: compiler/rustc_parse/src/validate_attr.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::parse_in;
44

55
use rustc_ast::tokenstream::{DelimSpan, TokenTree};
66
use rustc_ast::{self as ast, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
7-
use rustc_errors::{Applicability, PResult};
7+
use rustc_errors::{Applicability, FatalError, PResult};
88
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
99
use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
1010
use rustc_session::parse::ParseSess;
@@ -162,3 +162,15 @@ fn emit_malformed_attribute(
162162
.emit();
163163
}
164164
}
165+
166+
pub fn emit_fatal_malformed_builtin_attribute(
167+
sess: &ParseSess,
168+
attr: &Attribute,
169+
name: Symbol,
170+
) -> ! {
171+
let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").2;
172+
emit_malformed_attribute(sess, attr, name, template);
173+
// This is fatal, otherwise it will likely cause a cascade of other errors
174+
// (and an error here is expected to be very rare).
175+
FatalError.raise()
176+
}

Diff for: src/test/ui/invalid/invalid-crate-type-macro.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![crate_type = foo!()] //~ ERROR malformed `crate_type` attribute
2+
3+
macro_rules! foo {
4+
() => {"rlib"};
5+
}
6+
7+
fn main() {}

Diff for: src/test/ui/invalid/invalid-crate-type-macro.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: malformed `crate_type` attribute input
2+
--> $DIR/invalid-crate-type-macro.rs:1:1
3+
|
4+
LL | #![crate_type = foo!()]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![crate_type = "bin|lib|..."]`
6+
7+
error: aborting due to previous error
8+

Diff for: src/test/ui/modules/path-invalid-form.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[path = 123] //~ ERROR malformed `path` attribute
2+
mod foo;
3+
4+
fn main() {}

Diff for: src/test/ui/modules/path-invalid-form.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: malformed `path` attribute input
2+
--> $DIR/path-invalid-form.rs:1:1
3+
|
4+
LL | #[path = 123]
5+
| ^^^^^^^^^^^^^ help: must be of the form: `#[path = "file"]`
6+
7+
error: aborting due to previous error
8+

Diff for: src/test/ui/modules/path-macro.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
macro_rules! foo {
2+
() => {"bar.rs"};
3+
}
4+
5+
#[path = foo!()] //~ ERROR malformed `path` attribute
6+
mod abc;
7+
8+
fn main() {}

Diff for: src/test/ui/modules/path-macro.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: malformed `path` attribute input
2+
--> $DIR/path-macro.rs:5:1
3+
|
4+
LL | #[path = foo!()]
5+
| ^^^^^^^^^^^^^^^^ help: must be of the form: `#[path = "file"]`
6+
7+
error: aborting due to previous error
8+

Diff for: src/test/ui/recursion_limit/invalid_digit_type.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![recursion_limit = 123] //~ ERROR malformed `recursion_limit` attribute
2+
3+
fn main() {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: malformed `recursion_limit` attribute input
2+
--> $DIR/invalid_digit_type.rs:1:1
3+
|
4+
LL | #![recursion_limit = 123]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![recursion_limit = "N"]`
6+
7+
error: aborting due to previous error
8+

Diff for: src/test/ui/recursion_limit/invalid_macro.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![recursion_limit = foo!()] //~ ERROR malformed `recursion_limit` attribute
2+
3+
macro_rules! foo {
4+
() => {"128"};
5+
}
6+
7+
fn main() {}

Diff for: src/test/ui/recursion_limit/invalid_macro.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: malformed `recursion_limit` attribute input
2+
--> $DIR/invalid_macro.rs:1:1
3+
|
4+
LL | #![recursion_limit = foo!()]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#![recursion_limit = "N"]`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)