Skip to content

Commit b593085

Browse files
authored
Rollup merge of rust-lang#135247 - tgross35:stdlib-sym-list, r=oli-obk
Add a list of symbols for stable standard library crates There are a few locations where the crate name is checked against an enumerated list of `std`, `core`, `alloc`, and `proc_macro`, or some subset thereof. In most cases when we are looking for any "standard library" crate, all four crates should be treated the same. Change this so the crates are listed in one place, and that list is used wherever a list of `std` crates is needed. `test` could be considered relevant in some of these cases, but generally treating it separate from the others seems preferable while it is unstable. There are also a few places that Clippy will be able to use this.
2 parents 04e8715 + 933c4f5 commit b593085

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

compiler/rustc_expand/src/config.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_lint_defs::BuiltinLintDiag;
1818
use rustc_parse::validate_attr;
1919
use rustc_session::Session;
2020
use rustc_session::parse::feature_err;
21-
use rustc_span::{Span, Symbol, sym};
21+
use rustc_span::{STDLIB_STABLE_CRATES, Span, Symbol, sym};
2222
use thin_vec::ThinVec;
2323
use tracing::instrument;
2424

@@ -107,14 +107,11 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
107107

108108
// If the enabled feature is unstable, record it.
109109
if UNSTABLE_LANG_FEATURES.iter().find(|f| name == f.name).is_some() {
110-
// When the ICE comes from core, alloc or std (approximation of the standard
111-
// library), there's a chance that the person hitting the ICE may be using
112-
// -Zbuild-std or similar with an untested target. The bug is probably in the
113-
// standard library and not the compiler in that case, but that doesn't really
114-
// matter - we want a bug report.
115-
if features.internal(name)
116-
&& ![sym::core, sym::alloc, sym::std].contains(&crate_name)
117-
{
110+
// When the ICE comes a standard library crate, there's a chance that the person
111+
// hitting the ICE may be using -Zbuild-std or similar with an untested target.
112+
// The bug is probably in the standard library and not the compiler in that case,
113+
// but that doesn't really matter - we want a bug report.
114+
if features.internal(name) && !STDLIB_STABLE_CRATES.contains(&crate_name) {
118115
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
119116
}
120117

@@ -133,7 +130,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -
133130

134131
// Similar to above, detect internal lib features to suppress
135132
// the ICE message that asks for a report.
136-
if features.internal(name) && ![sym::core, sym::alloc, sym::std].contains(&crate_name) {
133+
if features.internal(name) && !STDLIB_STABLE_CRATES.contains(&crate_name) {
137134
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
138135
}
139136
}

compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_lint::{ARRAY_INTO_ITER, BOXED_SLICE_INTO_ITER};
88
use rustc_middle::span_bug;
99
use rustc_middle::ty::{self, Ty};
1010
use rustc_session::lint::builtin::{RUST_2021_PRELUDE_COLLISIONS, RUST_2024_PRELUDE_COLLISIONS};
11-
use rustc_span::{Ident, Span, kw, sym};
11+
use rustc_span::{Ident, STDLIB_STABLE_CRATES, Span, kw, sym};
1212
use rustc_trait_selection::infer::InferCtxtExt;
1313
use tracing::debug;
1414

@@ -76,7 +76,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7676
};
7777

7878
// No need to lint if method came from std/core, as that will now be in the prelude
79-
if matches!(self.tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core) {
79+
if STDLIB_STABLE_CRATES.contains(&self.tcx.crate_name(pick.item.def_id.krate)) {
8080
return;
8181
}
8282

@@ -252,7 +252,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
252252
}
253253

254254
// No need to lint if method came from std/core, as that will now be in the prelude
255-
if matches!(self.tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core) {
255+
if STDLIB_STABLE_CRATES.contains(&self.tcx.crate_name(pick.item.def_id.krate)) {
256256
return;
257257
}
258258

compiler/rustc_span/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mod span_encoding;
6767
pub use span_encoding::{DUMMY_SP, Span};
6868

6969
pub mod symbol;
70-
pub use symbol::{Ident, MacroRulesNormalizedIdent, Symbol, kw, sym};
70+
pub use symbol::{Ident, MacroRulesNormalizedIdent, STDLIB_STABLE_CRATES, Symbol, kw, sym};
7171

7272
mod analyze_source_file;
7373
pub mod fatal_error;

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,10 @@ symbols! {
22402240
}
22412241
}
22422242

2243+
/// Symbols for crates that are part of the stable standard library: `std`, `core`, `alloc`, and
2244+
/// `proc_macro`.
2245+
pub const STDLIB_STABLE_CRATES: &[Symbol] = &[sym::std, sym::core, sym::alloc, sym::proc_macro];
2246+
22432247
#[derive(Copy, Clone, Eq, HashStable_Generic, Encodable, Decodable)]
22442248
pub struct Ident {
22452249
pub name: Symbol,

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_middle::ty::{
2727
self, ToPolyTraitRef, TraitRef, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, Upcast,
2828
};
2929
use rustc_middle::{bug, span_bug};
30-
use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, sym};
30+
use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
3131
use tracing::{debug, instrument};
3232

3333
use super::on_unimplemented::{AppendConstMessage, OnUnimplementedNote};
@@ -520,7 +520,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
520520
match obligation.cause.span.ctxt().outer_expn_data().macro_def_id {
521521
Some(macro_def_id) => {
522522
let crate_name = tcx.crate_name(macro_def_id.krate);
523-
crate_name == sym::std || crate_name == sym::core
523+
STDLIB_STABLE_CRATES.contains(&crate_name)
524524
}
525525
None => false,
526526
};

0 commit comments

Comments
 (0)