Skip to content

Commit 0124066

Browse files
committed
Auto merge of rust-lang#133783 - matthiaskrgr:rollup-gl1ixcy, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#133089 (Stabilize noop_waker) - rust-lang#133522 (Don't suggest restricting bound with unstable traits on stable and mention it's unstable on nightly) - rust-lang#133545 (Lint against Symbol::intern on a string literal) - rust-lang#133558 (Structurally resolve in `probe_adt`) - rust-lang#133753 (Reduce false positives on some common cases from if-let-rescope lint) - rust-lang#133762 (stabilize const_{size,align}_of_val) - rust-lang#133777 (document -Zrandomize-layout in the unstable book) - rust-lang#133779 (Use correct `hir_id` for array const arg infers) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 41cbe3e + 0b5f3f1 commit 0124066

File tree

244 files changed

+807
-618
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+807
-618
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20112011
ExprKind::Underscore => {
20122012
if self.tcx.features().generic_arg_infer() {
20132013
let ct_kind = hir::ConstArgKind::Infer(self.lower_span(c.value.span));
2014-
self.arena.alloc(hir::ConstArg { hir_id: self.next_id(), kind: ct_kind })
2014+
self.arena
2015+
.alloc(hir::ConstArg { hir_id: self.lower_node_id(c.id), kind: ct_kind })
20152016
} else {
20162017
feature_err(
20172018
&self.tcx.sess,

compiler/rustc_ast_passes/src/feature_gate.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use rustc_ast::{NodeId, PatKind, attr, token};
44
use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute, Features, GateIssue};
55
use rustc_session::Session;
66
use rustc_session::parse::{feature_err, feature_err_issue, feature_warn};
7+
use rustc_span::Span;
78
use rustc_span::source_map::Spanned;
8-
use rustc_span::symbol::sym;
9-
use rustc_span::{Span, Symbol};
9+
use rustc_span::symbol::{Symbol, sym};
1010
use rustc_target::spec::abi;
1111
use thin_vec::ThinVec;
1212

@@ -690,6 +690,7 @@ fn check_new_solver_banned_features(sess: &Session, features: &Features) {
690690
.find(|feat| feat.gate_name == sym::generic_const_exprs)
691691
.map(|feat| feat.attr_sp)
692692
{
693+
#[cfg_attr(not(bootstrap), allow(rustc::symbol_intern_string_literal))]
693694
sess.dcx().emit_err(errors::IncompatibleFeatures {
694695
spans: vec![gce_span],
695696
f1: Symbol::intern("-Znext-solver=globally"),

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14501450
ty::Param(param_ty) => Ok((
14511451
generics.type_param(param_ty, tcx),
14521452
predicate.trait_ref.print_trait_sugared().to_string(),
1453+
Some(predicate.trait_ref.def_id),
14531454
)),
14541455
_ => Err(()),
14551456
}
@@ -1463,9 +1464,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14631464
tcx,
14641465
hir_generics,
14651466
err,
1466-
predicates
1467-
.iter()
1468-
.map(|(param, constraint)| (param.name.as_str(), &**constraint, None)),
1467+
predicates.iter().map(|(param, constraint, def_id)| {
1468+
(param.name.as_str(), &**constraint, *def_id)
1469+
}),
14691470
None,
14701471
);
14711472
}

compiler/rustc_borrowck/src/universal_regions.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use rustc_middle::ty::{
3333
TyCtxt, TypeVisitableExt,
3434
};
3535
use rustc_middle::{bug, span_bug};
36+
use rustc_span::ErrorGuaranteed;
3637
use rustc_span::symbol::{kw, sym};
37-
use rustc_span::{ErrorGuaranteed, Symbol};
3838
use tracing::{debug, instrument};
3939

4040
use crate::BorrowckInferCtxt;
@@ -524,7 +524,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
524524

525525
let reg_vid = self
526526
.infcx
527-
.next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("c-variadic")))
527+
.next_nll_region_var(FR, || RegionCtxt::Free(sym::c_dash_variadic))
528528
.as_var();
529529

530530
let region = ty::Region::new_var(self.infcx.tcx, reg_vid);
@@ -540,10 +540,8 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
540540
}
541541
}
542542

543-
let fr_fn_body = self
544-
.infcx
545-
.next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("fn_body")))
546-
.as_var();
543+
let fr_fn_body =
544+
self.infcx.next_nll_region_var(FR, || RegionCtxt::Free(sym::fn_body)).as_var();
547545

548546
let num_universals = self.infcx.num_region_vars();
549547

compiler/rustc_codegen_cranelift/src/driver/jit.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn create_jit_module(
7474
jit_builder.symbol("__clif_jit_fn", clif_jit_fn as *const u8);
7575
let mut jit_module = UnwindModule::new(JITModule::new(jit_builder), false);
7676

77-
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, Symbol::intern("dummy_cgu_name"));
77+
let cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);
7878

7979
crate::allocator::codegen(tcx, &mut jit_module);
8080

@@ -276,12 +276,7 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) ->
276276

277277
jit_module.module.prepare_for_function_redefine(func_id).unwrap();
278278

279-
let mut cx = crate::CodegenCx::new(
280-
tcx,
281-
jit_module.isa(),
282-
false,
283-
Symbol::intern("dummy_cgu_name"),
284-
);
279+
let mut cx = crate::CodegenCx::new(tcx, jit_module.isa(), false, sym::dummy_cgu_name);
285280
codegen_and_compile_fn(tcx, &mut cx, &mut Context::new(), jit_module, instance);
286281

287282
assert!(cx.global_asm.is_empty());

compiler/rustc_codegen_cranelift/src/lib.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -189,18 +189,13 @@ impl CodegenBackend for CraneliftCodegenBackend {
189189
// FIXME return the actually used target features. this is necessary for #[cfg(target_feature)]
190190
if sess.target.arch == "x86_64" && sess.target.os != "none" {
191191
// x86_64 mandates SSE2 support
192-
vec![Symbol::intern("fxsr"), sym::sse, Symbol::intern("sse2")]
192+
vec![sym::fsxr, sym::sse, sym::sse2]
193193
} else if sess.target.arch == "aarch64" {
194194
match &*sess.target.os {
195195
"none" => vec![],
196196
// On macOS the aes, sha2 and sha3 features are enabled by default and ring
197197
// fails to compile on macOS when they are not present.
198-
"macos" => vec![
199-
sym::neon,
200-
Symbol::intern("aes"),
201-
Symbol::intern("sha2"),
202-
Symbol::intern("sha3"),
203-
],
198+
"macos" => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
204199
// AArch64 mandates Neon support
205200
_ => vec![sym::neon],
206201
}

compiler/rustc_const_eval/src/check_consts/ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
140140
err,
141141
param_ty.name.as_str(),
142142
&constraint,
143-
None,
143+
Some(trait_ref.def_id),
144144
None,
145145
);
146146
}

compiler/rustc_hir_analysis/src/collect/generics_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
419419
if let Node::ConstBlock(_) = node {
420420
own_params.push(ty::GenericParamDef {
421421
index: next_index(),
422-
name: Symbol::intern("<const_ty>"),
422+
name: rustc_span::sym::const_ty_placeholder,
423423
def_id: def_id.to_def_id(),
424424
pure_wrt_drop: false,
425425
kind: ty::GenericParamDefKind::Type { has_default: false, synthetic: false },

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
279279
} else {
280280
let mut err = self.dcx().create_err(err);
281281
if suggest_constraining_type_param(
282-
tcx, generics, &mut err, &qself_str, &trait_ref, None, None,
282+
tcx,
283+
generics,
284+
&mut err,
285+
&qself_str,
286+
&trait_ref,
287+
Some(best_trait),
288+
None,
283289
) && !identically_named
284290
{
285291
// We suggested constraining a type parameter, but the associated item on it

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
307307
ty::Alias(ty::Projection | ty::Inherent | ty::Weak, _)
308308
if !ty.has_escaping_bound_vars() =>
309309
{
310-
self.normalize(span, ty).ty_adt_def()
310+
if self.next_trait_solver() {
311+
self.try_structurally_resolve_type(span, ty).ty_adt_def()
312+
} else {
313+
self.normalize(span, ty).ty_adt_def()
314+
}
311315
}
312316
_ => None,
313317
}

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
664664
let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
665665
tcx.is_diagnostic_item(sym::write_macro, def_id)
666666
|| tcx.is_diagnostic_item(sym::writeln_macro, def_id)
667-
}) && item_name.name == Symbol::intern("write_fmt");
667+
}) && item_name.name == sym::write_fmt;
668668
let mut err = if is_write && let SelfSource::MethodCall(rcvr_expr) = source {
669669
self.suggest_missing_writer(rcvr_ty, rcvr_expr)
670670
} else {

compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ lint_suspicious_double_ref_clone =
775775
lint_suspicious_double_ref_deref =
776776
using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
777777
778+
lint_symbol_intern_string_literal = using `Symbol::intern` on a string literal
779+
.help = consider adding the symbol to `compiler/rustc_span/src/symbol.rs`
780+
778781
lint_trailing_semi_macro = trailing semicolon in macro used in expression position
779782
.note1 = macro invocations at the end of a block are treated as expressions
780783
.note2 = to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}`

compiler/rustc_lint/src/if_let_rescope.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ fn expr_parent_is_else(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
103103
}
104104

105105
fn expr_parent_is_stmt(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
106-
let Some((_, hir::Node::Stmt(stmt))) = tcx.hir().parent_iter(hir_id).next() else {
107-
return false;
106+
let mut parents = tcx.hir().parent_iter(hir_id);
107+
let stmt = match parents.next() {
108+
Some((_, hir::Node::Stmt(stmt))) => stmt,
109+
Some((_, hir::Node::Block(_) | hir::Node::Arm(_))) => return true,
110+
_ => return false,
108111
};
109112
let (hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr)) = stmt.kind else { return false };
110113
expr.hir_id == hir_id

compiler/rustc_lint/src/internal.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use tracing::debug;
1717

1818
use crate::lints::{
1919
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword,
20-
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag, TyQualified,
21-
TykindDiag, TykindKind, TypeIrInherentUsage, UntranslatableDiag,
20+
NonGlobImportTypeIrInherent, QueryInstability, QueryUntracked, SpanUseEqCtxtDiag,
21+
SymbolInternStringLiteralDiag, TyQualified, TykindDiag, TykindKind, TypeIrInherentUsage,
22+
UntranslatableDiag,
2223
};
2324
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2425

@@ -650,3 +651,33 @@ fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
650651
_ => false,
651652
}
652653
}
654+
655+
declare_tool_lint! {
656+
/// The `symbol_intern_string_literal` detects `Symbol::intern` being called on a string literal
657+
pub rustc::SYMBOL_INTERN_STRING_LITERAL,
658+
// rustc_driver crates out of the compiler can't/shouldn't add preinterned symbols;
659+
// bootstrap will deny this manually
660+
Allow,
661+
"Forbid uses of string literals in `Symbol::intern`, suggesting preinterning instead",
662+
report_in_external_macro: true
663+
}
664+
665+
declare_lint_pass!(SymbolInternStringLiteral => [SYMBOL_INTERN_STRING_LITERAL]);
666+
667+
impl<'tcx> LateLintPass<'tcx> for SymbolInternStringLiteral {
668+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx>) {
669+
if let ExprKind::Call(path, [arg]) = expr.kind
670+
&& let ExprKind::Path(ref qpath) = path.kind
671+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
672+
&& cx.tcx.is_diagnostic_item(sym::SymbolIntern, def_id)
673+
&& let ExprKind::Lit(kind) = arg.kind
674+
&& let rustc_ast::LitKind::Str(_, _) = kind.node
675+
{
676+
cx.emit_span_lint(
677+
SYMBOL_INTERN_STRING_LITERAL,
678+
kind.span,
679+
SymbolInternStringLiteralDiag,
680+
);
681+
}
682+
}
683+
}

compiler/rustc_lint/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ fn register_internals(store: &mut LintStore) {
614614
store.register_late_mod_pass(|_| Box::new(PassByValue));
615615
store.register_lints(&SpanUseEqCtxt::lint_vec());
616616
store.register_late_mod_pass(|_| Box::new(SpanUseEqCtxt));
617+
store.register_lints(&SymbolInternStringLiteral::lint_vec());
618+
store.register_late_mod_pass(|_| Box::new(SymbolInternStringLiteral));
617619
// FIXME(davidtwco): deliberately do not include `UNTRANSLATABLE_DIAGNOSTIC` and
618620
// `DIAGNOSTIC_OUTSIDE_OF_IMPL` here because `-Wrustc::internal` is provided to every crate and
619621
// these lints will trigger all of the time - change this once migration to diagnostic structs

compiler/rustc_lint/src/lints.rs

+5
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,11 @@ pub(crate) struct QueryUntracked {
907907
#[diag(lint_span_use_eq_ctxt)]
908908
pub(crate) struct SpanUseEqCtxtDiag;
909909

910+
#[derive(LintDiagnostic)]
911+
#[diag(lint_symbol_intern_string_literal)]
912+
#[help]
913+
pub(crate) struct SymbolInternStringLiteralDiag;
914+
910915
#[derive(LintDiagnostic)]
911916
#[diag(lint_tykind_kind)]
912917
pub(crate) struct TykindKind {

compiler/rustc_lint/src/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ impl UnusedImportBraces {
15631563
}
15641564
rename.unwrap_or(orig_ident).name
15651565
}
1566-
ast::UseTreeKind::Glob => Symbol::intern("*"),
1566+
ast::UseTreeKind::Glob => sym::asterisk,
15671567
ast::UseTreeKind::Nested { .. } => return,
15681568
};
15691569

compiler/rustc_metadata/src/creader.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,10 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
861861
// First up we check for global allocators. Look at the crate graph here
862862
// and see what's a global allocator, including if we ourselves are a
863863
// global allocator.
864-
let mut global_allocator =
865-
self.cstore.has_global_allocator.then(|| Symbol::intern("this crate"));
864+
#[cfg_attr(not(bootstrap), allow(rustc::symbol_intern_string_literal))]
865+
let this_crate = Symbol::intern("this crate");
866+
867+
let mut global_allocator = self.cstore.has_global_allocator.then_some(this_crate);
866868
for (_, data) in self.cstore.iter_crate_data() {
867869
if data.has_global_allocator() {
868870
match global_allocator {
@@ -876,8 +878,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
876878
}
877879
}
878880
}
879-
let mut alloc_error_handler =
880-
self.cstore.has_alloc_error_handler.then(|| Symbol::intern("this crate"));
881+
let mut alloc_error_handler = self.cstore.has_alloc_error_handler.then_some(this_crate);
881882
for (_, data) in self.cstore.iter_crate_data() {
882883
if data.has_alloc_error_handler() {
883884
match alloc_error_handler {

compiler/rustc_metadata/src/rmeta/decoder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,7 @@ impl MetadataBlob {
872872

873873
let def_kind = root.tables.def_kind.get(blob, item).unwrap();
874874
let def_key = root.tables.def_keys.get(blob, item).unwrap().decode(blob);
875+
#[cfg_attr(not(bootstrap), allow(rustc::symbol_intern_string_literal))]
875876
let def_name = if item == CRATE_DEF_INDEX {
876877
rustc_span::symbol::kw::Crate
877878
} else {

compiler/rustc_metadata/src/rmeta/encoder.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1389,10 +1389,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13891389
// `ConstArgKind::Path`. We never actually access this `DefId`
13901390
// anywhere so we don't need to encode it for other crates.
13911391
if def_kind == DefKind::AnonConst
1392-
&& matches!(
1393-
tcx.hir_node_by_def_id(local_id),
1394-
hir::Node::ConstArg(hir::ConstArg { kind: hir::ConstArgKind::Path(..), .. })
1395-
)
1392+
&& match tcx.hir_node_by_def_id(local_id) {
1393+
hir::Node::ConstArg(hir::ConstArg { kind, .. }) => match kind {
1394+
// Skip encoding defs for these as they should not have had a `DefId` created
1395+
hir::ConstArgKind::Path(..) | hir::ConstArgKind::Infer(..) => true,
1396+
hir::ConstArgKind::Anon(..) => false,
1397+
},
1398+
_ => false,
1399+
}
13961400
{
13971401
continue;
13981402
}

0 commit comments

Comments
 (0)