@@ -29,10 +29,12 @@ use rustc_resolve::Resolver;
29
29
use rustc_session:: code_stats:: VTableSizeInfo ;
30
30
use rustc_session:: config:: { CrateType , Input , OutFileName , OutputFilenames , OutputType } ;
31
31
use rustc_session:: cstore:: Untracked ;
32
- use rustc_session:: output:: { collect_crate_types, filename_for_input, find_crate_name } ;
32
+ use rustc_session:: output:: { collect_crate_types, filename_for_input} ;
33
33
use rustc_session:: search_paths:: PathKind ;
34
34
use rustc_session:: { Limit , Session } ;
35
- use rustc_span:: { ErrorGuaranteed , FileName , SourceFileHash , SourceFileHashAlgorithm , Symbol , sym} ;
35
+ use rustc_span:: {
36
+ ErrorGuaranteed , FileName , SourceFileHash , SourceFileHashAlgorithm , Span , Symbol , sym,
37
+ } ;
36
38
use rustc_target:: spec:: PanicStrategy ;
37
39
use rustc_trait_selection:: traits;
38
40
use tracing:: { info, instrument} ;
@@ -725,8 +727,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
725
727
726
728
let pre_configured_attrs = rustc_expand:: config:: pre_configure_attrs ( sess, & krate. attrs ) ;
727
729
728
- // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
729
- let crate_name = find_crate_name ( sess, & pre_configured_attrs) ;
730
+ let crate_name = get_crate_name ( sess, & pre_configured_attrs) ;
730
731
let crate_types = collect_crate_types ( sess, & pre_configured_attrs) ;
731
732
let stable_crate_id = StableCrateId :: new (
732
733
crate_name,
@@ -735,7 +736,7 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
735
736
sess. cfg_version ,
736
737
) ;
737
738
let outputs = util:: build_output_filenames ( & pre_configured_attrs, sess) ;
738
- let dep_graph = setup_dep_graph ( sess) ;
739
+ let dep_graph = setup_dep_graph ( sess, crate_name ) ;
739
740
740
741
let cstore =
741
742
FreezeLock :: new ( Box :: new ( CStore :: new ( compiler. codegen_backend . metadata_loader ( ) ) ) as _ ) ;
@@ -1162,23 +1163,83 @@ pub(crate) fn start_codegen<'tcx>(
1162
1163
codegen
1163
1164
}
1164
1165
1165
- fn get_recursion_limit ( krate_attrs : & [ ast:: Attribute ] , sess : & Session ) -> Limit {
1166
- if let Some ( attr) = krate_attrs
1167
- . iter ( )
1168
- . find ( |attr| attr. has_name ( sym:: recursion_limit) && attr. value_str ( ) . is_none ( ) )
1166
+ /// Compute and validate the crate name.
1167
+ pub fn get_crate_name ( sess : & Session , krate_attrs : & [ ast:: Attribute ] ) -> Symbol {
1168
+ // We unconditionally validate all `#![crate_name]`s even if a crate name was
1169
+ // set on the command line via `--crate-name` which we prioritize over the
1170
+ // crate attributes. We perform the validation here instead of later to ensure
1171
+ // it gets run in all code paths requiring the crate name very early on.
1172
+ // Namely, print requests (`--print`).
1173
+ let attr_crate_name =
1174
+ validate_and_find_value_str_builtin_attr ( sym:: crate_name, sess, krate_attrs) ;
1175
+
1176
+ let validate = |name, span| {
1177
+ rustc_session:: output:: validate_crate_name ( sess, name, span) ;
1178
+ name
1179
+ } ;
1180
+
1181
+ if let Some ( crate_name) = & sess. opts . crate_name {
1182
+ let crate_name = Symbol :: intern ( crate_name) ;
1183
+ if let Some ( ( attr_crate_name, span) ) = attr_crate_name
1184
+ && attr_crate_name != crate_name
1185
+ {
1186
+ sess. dcx ( ) . emit_err ( errors:: CrateNameDoesNotMatch {
1187
+ span,
1188
+ crate_name,
1189
+ attr_crate_name,
1190
+ } ) ;
1191
+ }
1192
+ return validate ( crate_name, None ) ;
1193
+ }
1194
+
1195
+ if let Some ( ( crate_name, span) ) = attr_crate_name {
1196
+ return validate ( crate_name, Some ( span) ) ;
1197
+ }
1198
+
1199
+ if let Input :: File ( ref path) = sess. io . input
1200
+ && let Some ( file_stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) )
1169
1201
{
1170
- // This is here mainly to check for using a macro, such as
1171
- // #![recursion_limit = foo!()]. That is not supported since that
1172
- // would require expanding this while in the middle of expansion,
1173
- // which needs to know the limit before expanding. Otherwise,
1174
- // validation would normally be caught in AstValidator (via
1175
- // `check_builtin_attribute`), but by the time that runs the macro
1176
- // is expanded, and it doesn't give an error.
1177
- validate_attr:: emit_fatal_malformed_builtin_attribute (
1178
- & sess. psess ,
1179
- attr,
1180
- sym:: recursion_limit,
1181
- ) ;
1202
+ if file_stem. starts_with ( '-' ) {
1203
+ sess. dcx ( ) . emit_err ( errors:: CrateNameInvalid { crate_name : file_stem } ) ;
1204
+ } else {
1205
+ return validate ( Symbol :: intern ( & file_stem. replace ( '-' , "_" ) ) , None ) ;
1206
+ }
1182
1207
}
1208
+
1209
+ sym:: rust_out
1210
+ }
1211
+
1212
+ fn get_recursion_limit ( krate_attrs : & [ ast:: Attribute ] , sess : & Session ) -> Limit {
1213
+ // We don't permit macro calls inside of the attribute (e.g., #![recursion_limit = `expand!()`])
1214
+ // because that would require expanding this while in the middle of expansion, which needs to
1215
+ // know the limit before expanding.
1216
+ let _ = validate_and_find_value_str_builtin_attr ( sym:: recursion_limit, sess, krate_attrs) ;
1183
1217
rustc_middle:: middle:: limits:: get_recursion_limit ( krate_attrs, sess)
1184
1218
}
1219
+
1220
+ /// Validate *all* occurrences of the given "[value-str]" built-in attribute and return the first find.
1221
+ ///
1222
+ /// This validator is intended for built-in attributes whose value needs to be known very early
1223
+ /// during compilation (namely, before macro expansion) and it mainly exists to reject macro calls
1224
+ /// inside of the attributes, such as in `#![name = expand!()]`. Normal attribute validation happens
1225
+ /// during semantic analysis via [`TyCtxt::check_mod_attrs`] which happens *after* macro expansion
1226
+ /// when such macro calls (here: `expand`) have already been expanded and we can no longer check for
1227
+ /// their presence.
1228
+ ///
1229
+ /// [value-str]: ast::Attribute::value_str
1230
+ fn validate_and_find_value_str_builtin_attr (
1231
+ name : Symbol ,
1232
+ sess : & Session ,
1233
+ krate_attrs : & [ ast:: Attribute ] ,
1234
+ ) -> Option < ( Symbol , Span ) > {
1235
+ let mut result = None ;
1236
+ // Validate *all* relevant attributes, not just the first occurrence.
1237
+ for attr in ast:: attr:: filter_by_name ( krate_attrs, name) {
1238
+ let Some ( value) = attr. value_str ( ) else {
1239
+ validate_attr:: emit_fatal_malformed_builtin_attribute ( & sess. psess , attr, name)
1240
+ } ;
1241
+ // Choose the first occurrence as our result.
1242
+ result. get_or_insert ( ( value, attr. span ) ) ;
1243
+ }
1244
+ result
1245
+ }
0 commit comments