@@ -29,11 +29,11 @@ use rustc_resolve::Resolver;
2929use rustc_session:: code_stats:: VTableSizeInfo ;
3030use rustc_session:: config:: { CrateType , Input , OutFileName , OutputFilenames , OutputType } ;
3131use 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} ;
3333use rustc_session:: search_paths:: PathKind ;
3434use rustc_session:: { Limit , Session } ;
3535use rustc_span:: symbol:: { Symbol , sym} ;
36- use rustc_span:: { ErrorGuaranteed , FileName , SourceFileHash , SourceFileHashAlgorithm } ;
36+ use rustc_span:: { ErrorGuaranteed , FileName , SourceFileHash , SourceFileHashAlgorithm , Span } ;
3737use rustc_target:: spec:: PanicStrategy ;
3838use rustc_trait_selection:: traits;
3939use tracing:: { info, instrument} ;
@@ -726,8 +726,7 @@ pub(crate) fn create_global_ctxt<'tcx>(
726726
727727 let pre_configured_attrs = rustc_expand:: config:: pre_configure_attrs ( sess, & krate. attrs ) ;
728728
729- // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
730- let crate_name = find_crate_name ( sess, & pre_configured_attrs) ;
729+ let crate_name = get_crate_name ( sess, & pre_configured_attrs) ;
731730 let crate_types = collect_crate_types ( sess, & pre_configured_attrs) ;
732731 let stable_crate_id = StableCrateId :: new (
733732 crate_name,
@@ -736,7 +735,7 @@ pub(crate) fn create_global_ctxt<'tcx>(
736735 sess. cfg_version ,
737736 ) ;
738737 let outputs = util:: build_output_filenames ( & pre_configured_attrs, sess) ;
739- let dep_graph = setup_dep_graph ( sess) ;
738+ let dep_graph = setup_dep_graph ( sess, crate_name ) ;
740739
741740 let cstore =
742741 FreezeLock :: new ( Box :: new ( CStore :: new ( compiler. codegen_backend . metadata_loader ( ) ) ) as _ ) ;
@@ -1128,23 +1127,83 @@ pub(crate) fn start_codegen<'tcx>(
11281127 codegen
11291128}
11301129
1131- fn get_recursion_limit ( krate_attrs : & [ ast:: Attribute ] , sess : & Session ) -> Limit {
1132- if let Some ( attr) = krate_attrs
1133- . iter ( )
1134- . find ( |attr| attr. has_name ( sym:: recursion_limit) && attr. value_str ( ) . is_none ( ) )
1130+ /// Compute and validate the crate name.
1131+ pub fn get_crate_name ( sess : & Session , krate_attrs : & [ ast:: Attribute ] ) -> Symbol {
1132+ // We unconditionally validate all `#![crate_name]`s even if a crate name was
1133+ // set on the command line via `--crate-name` which we prioritize over the
1134+ // crate attributes. We perform the validation here instead of later to ensure
1135+ // it gets run in all code paths requiring the crate name very early on.
1136+ // Namely, print requests (`--print`).
1137+ let attr_crate_name =
1138+ validate_and_find_value_str_builtin_attr ( sym:: crate_name, sess, krate_attrs) ;
1139+
1140+ let validate = |name, span| {
1141+ rustc_session:: output:: validate_crate_name ( sess, name, span) ;
1142+ name
1143+ } ;
1144+
1145+ if let Some ( crate_name) = & sess. opts . crate_name {
1146+ let crate_name = Symbol :: intern ( crate_name) ;
1147+ if let Some ( ( attr_crate_name, span) ) = attr_crate_name
1148+ && attr_crate_name != crate_name
1149+ {
1150+ sess. dcx ( ) . emit_err ( errors:: CrateNameDoesNotMatch {
1151+ span,
1152+ crate_name,
1153+ attr_crate_name,
1154+ } ) ;
1155+ }
1156+ return validate ( crate_name, None ) ;
1157+ }
1158+
1159+ if let Some ( ( crate_name, span) ) = attr_crate_name {
1160+ return validate ( crate_name, Some ( span) ) ;
1161+ }
1162+
1163+ if let Input :: File ( ref path) = sess. io . input
1164+ && let Some ( file_stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) )
11351165 {
1136- // This is here mainly to check for using a macro, such as
1137- // #![recursion_limit = foo!()]. That is not supported since that
1138- // would require expanding this while in the middle of expansion,
1139- // which needs to know the limit before expanding. Otherwise,
1140- // validation would normally be caught in AstValidator (via
1141- // `check_builtin_attribute`), but by the time that runs the macro
1142- // is expanded, and it doesn't give an error.
1143- validate_attr:: emit_fatal_malformed_builtin_attribute (
1144- & sess. psess ,
1145- attr,
1146- sym:: recursion_limit,
1147- ) ;
1166+ if file_stem. starts_with ( '-' ) {
1167+ sess. dcx ( ) . emit_err ( errors:: CrateNameInvalid { crate_name : file_stem } ) ;
1168+ } else {
1169+ return validate ( Symbol :: intern ( & file_stem. replace ( '-' , "_" ) ) , None ) ;
1170+ }
11481171 }
1172+
1173+ sym:: rust_out
1174+ }
1175+
1176+ fn get_recursion_limit ( krate_attrs : & [ ast:: Attribute ] , sess : & Session ) -> Limit {
1177+ // We don't permit macro calls inside of the attribute (e.g., #![recursion_limit = `expand!()`])
1178+ // because that would require expanding this while in the middle of expansion, which needs to
1179+ // know the limit before expanding.
1180+ let _ = validate_and_find_value_str_builtin_attr ( sym:: recursion_limit, sess, krate_attrs) ;
11491181 rustc_middle:: middle:: limits:: get_recursion_limit ( krate_attrs, sess)
11501182}
1183+
1184+ /// Validate *all* occurrences of the given "[value-str]" built-in attribute and return the first find.
1185+ ///
1186+ /// This validator is intended for built-in attributes whose value needs to be known very early
1187+ /// during compilation (namely, before macro expansion) and it mainly exists to reject macro calls
1188+ /// inside of the attributes, such as in `#![name = expand!()]`. Normal attribute validation happens
1189+ /// during semantic analysis via [`TyCtxt::check_mod_attrs`] which happens *after* macro expansion
1190+ /// when such macro calls (here: `expand`) have already been expanded and we can no longer check for
1191+ /// their presence.
1192+ ///
1193+ /// [value-str]: ast::Attribute::value_str
1194+ fn validate_and_find_value_str_builtin_attr (
1195+ name : Symbol ,
1196+ sess : & Session ,
1197+ krate_attrs : & [ ast:: Attribute ] ,
1198+ ) -> Option < ( Symbol , Span ) > {
1199+ let mut result = None ;
1200+ // Validate *all* relevant attributes, not just the first occurrence.
1201+ for attr in ast:: attr:: filter_by_name ( krate_attrs, name) {
1202+ let Some ( value) = attr. value_str ( ) else {
1203+ validate_attr:: emit_fatal_malformed_builtin_attribute ( & sess. psess , attr, name)
1204+ } ;
1205+ // Choose the first occurrence as our result.
1206+ result. get_or_insert ( ( value, attr. span ) ) ;
1207+ }
1208+ result
1209+ }
0 commit comments