@@ -29,11 +29,10 @@ 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:: symbol:: { sym, Symbol } ;
36
- use rustc_span:: FileName ;
35
+ use rustc_span:: { sym, FileName , Span , Symbol } ;
37
36
use rustc_target:: spec:: PanicStrategy ;
38
37
use rustc_trait_selection:: traits;
39
38
use tracing:: { info, instrument} ;
@@ -663,8 +662,7 @@ pub(crate) fn create_global_ctxt<'tcx>(
663
662
664
663
let pre_configured_attrs = rustc_expand:: config:: pre_configure_attrs ( sess, & krate. attrs ) ;
665
664
666
- // parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
667
- let crate_name = find_crate_name ( sess, & pre_configured_attrs) ;
665
+ let crate_name = get_crate_name ( sess, & pre_configured_attrs) ;
668
666
let crate_types = collect_crate_types ( sess, & pre_configured_attrs) ;
669
667
let stable_crate_id = StableCrateId :: new (
670
668
crate_name,
@@ -673,7 +671,7 @@ pub(crate) fn create_global_ctxt<'tcx>(
673
671
sess. cfg_version ,
674
672
) ;
675
673
let outputs = util:: build_output_filenames ( & pre_configured_attrs, sess) ;
676
- let dep_graph = setup_dep_graph ( sess) ?;
674
+ let dep_graph = setup_dep_graph ( sess, crate_name ) ?;
677
675
678
676
let cstore =
679
677
FreezeLock :: new ( Box :: new ( CStore :: new ( compiler. codegen_backend . metadata_loader ( ) ) ) as _ ) ;
@@ -1074,23 +1072,83 @@ pub(crate) fn start_codegen<'tcx>(
1074
1072
Ok ( codegen)
1075
1073
}
1076
1074
1077
- fn get_recursion_limit ( krate_attrs : & [ ast:: Attribute ] , sess : & Session ) -> Limit {
1078
- if let Some ( attr) = krate_attrs
1079
- . iter ( )
1080
- . find ( |attr| attr. has_name ( sym:: recursion_limit) && attr. value_str ( ) . is_none ( ) )
1075
+ /// Compute and validate the crate name.
1076
+ pub fn get_crate_name ( sess : & Session , krate_attrs : & [ ast:: Attribute ] ) -> Symbol {
1077
+ // We unconditionally validate all `#![crate_name]`s even if a crate name was
1078
+ // set on the command line via `--crate-name` which we prioritize over the
1079
+ // crate attributes. We perform the validation here instead of later to ensure
1080
+ // it gets run in all code paths requiring the crate name very early on.
1081
+ // Namely, print requests (`--print`).
1082
+ let attr_crate_name =
1083
+ validate_and_find_value_str_builtin_attr ( sym:: crate_name, sess, krate_attrs) ;
1084
+
1085
+ let validate = |name, span| {
1086
+ rustc_session:: output:: validate_crate_name ( sess, name, span) ;
1087
+ name
1088
+ } ;
1089
+
1090
+ if let Some ( crate_name) = & sess. opts . crate_name {
1091
+ let crate_name = Symbol :: intern ( crate_name) ;
1092
+ if let Some ( ( attr_crate_name, span) ) = attr_crate_name
1093
+ && attr_crate_name != crate_name
1094
+ {
1095
+ sess. dcx ( ) . emit_err ( errors:: CrateNameDoesNotMatch {
1096
+ span,
1097
+ crate_name,
1098
+ attr_crate_name,
1099
+ } ) ;
1100
+ }
1101
+ return validate ( crate_name, None ) ;
1102
+ }
1103
+
1104
+ if let Some ( ( crate_name, span) ) = attr_crate_name {
1105
+ return validate ( crate_name, Some ( span) ) ;
1106
+ }
1107
+
1108
+ if let Input :: File ( ref path) = sess. io . input
1109
+ && let Some ( file_stem) = path. file_stem ( ) . and_then ( |s| s. to_str ( ) )
1081
1110
{
1082
- // This is here mainly to check for using a macro, such as
1083
- // #![recursion_limit = foo!()]. That is not supported since that
1084
- // would require expanding this while in the middle of expansion,
1085
- // which needs to know the limit before expanding. Otherwise,
1086
- // validation would normally be caught in AstValidator (via
1087
- // `check_builtin_attribute`), but by the time that runs the macro
1088
- // is expanded, and it doesn't give an error.
1089
- validate_attr:: emit_fatal_malformed_builtin_attribute (
1090
- & sess. psess ,
1091
- attr,
1092
- sym:: recursion_limit,
1093
- ) ;
1111
+ if file_stem. starts_with ( '-' ) {
1112
+ sess. dcx ( ) . emit_err ( errors:: CrateNameInvalid { crate_name : file_stem } ) ;
1113
+ } else {
1114
+ return validate ( Symbol :: intern ( & file_stem. replace ( '-' , "_" ) ) , None ) ;
1115
+ }
1094
1116
}
1117
+
1118
+ Symbol :: intern ( "rust_out" )
1119
+ }
1120
+
1121
+ fn get_recursion_limit ( krate_attrs : & [ ast:: Attribute ] , sess : & Session ) -> Limit {
1122
+ // We don't permit macro calls inside of the attribute (e.g., #![recursion_limit = `expand!()`])
1123
+ // because that would require expanding this while in the middle of expansion, which needs to
1124
+ // know the limit before expanding.
1125
+ let _ = validate_and_find_value_str_builtin_attr ( sym:: recursion_limit, sess, krate_attrs) ;
1095
1126
rustc_middle:: middle:: limits:: get_recursion_limit ( krate_attrs, sess)
1096
1127
}
1128
+
1129
+ /// Validate *all* occurrences of the given "[value-str]" built-in attribute and return the first find.
1130
+ ///
1131
+ /// This validator is intended for built-in attributes whose value needs to be known very early
1132
+ /// during compilation (namely, before macro expansion) and it mainly exists to reject macro calls
1133
+ /// inside of the attributes, such as in `#![name = expand!()]`. Normal attribute validation happens
1134
+ /// during semantic analysis via [`TyCtxt::check_mod_attrs`] which happens *after* macro expansion
1135
+ /// when such macro calls (here: `expand`) have already been expanded and we can no longer check for
1136
+ /// their presence.
1137
+ ///
1138
+ /// [value-str]: ast::Attribute::value_str
1139
+ fn validate_and_find_value_str_builtin_attr (
1140
+ name : Symbol ,
1141
+ sess : & Session ,
1142
+ krate_attrs : & [ ast:: Attribute ] ,
1143
+ ) -> Option < ( Symbol , Span ) > {
1144
+ let mut result = None ;
1145
+ // Validate *all* relevant attributes, not just the first occurrence.
1146
+ for attr in ast:: attr:: filter_by_name ( krate_attrs, name) {
1147
+ let Some ( value) = attr. value_str ( ) else {
1148
+ validate_attr:: emit_fatal_malformed_builtin_attribute ( & sess. psess , attr, name)
1149
+ } ;
1150
+ // Choose the first occurrence as our result.
1151
+ result. get_or_insert ( ( value, attr. span ) ) ;
1152
+ }
1153
+ result
1154
+ }
0 commit comments