Skip to content

Commit 6bdfdf5

Browse files
committed
Auto merge of rust-lang#116848 - matthiaskrgr:rollup-y4e025o, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#116717 (Special case iterator chain checks for suggestion) - rust-lang#116719 (Add MonoItems and Instance to stable_mir) - rust-lang#116787 (Implement an internal lint encouraging use of `Span::eq_ctxt`) - rust-lang#116827 (Make `handle_options` public again.) - rust-lang#116838 (Fix duplicate labels emitted in `render_multispan_macro_backtrace()`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 93e62a2 + 19d100b commit 6bdfdf5

Some content is hidden

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

42 files changed

+900
-108
lines changed

compiler/rustc_driver_impl/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,11 @@ fn print_flag_list<T>(
11851185
///
11861186
/// So with all that in mind, the comments below have some more detail about the
11871187
/// contortions done here to get things to work out correctly.
1188-
fn handle_options(handler: &EarlyErrorHandler, args: &[String]) -> Option<getopts::Matches> {
1188+
///
1189+
/// This does not need to be `pub` for rustc itself, but @chaosite needs it to
1190+
/// be public when using rustc as a library, see
1191+
/// <https://github.com/rust-lang/rust/commit/2b4c33817a5aaecabf4c6598d41e190080ec119e>
1192+
pub fn handle_options(handler: &EarlyErrorHandler, args: &[String]) -> Option<getopts::Matches> {
11891193
if args.is_empty() {
11901194
// user did not write `-v` nor `-Z unstable-options`, so do not
11911195
// include that extra information.

compiler/rustc_errors/src/emitter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
use rustc_lint_defs::pluralize;
2424

2525
use derive_setters::Setters;
26-
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
26+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
2727
use rustc_data_structures::sync::{DynSend, IntoDynSyncSend, Lrc};
2828
use rustc_error_messages::{FluentArgs, SpanLabel};
2929
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -370,7 +370,7 @@ pub trait Emitter: Translate {
370370
}
371371

372372
fn render_multispan_macro_backtrace(&self, span: &mut MultiSpan, always_backtrace: bool) {
373-
let mut new_labels: Vec<(Span, String)> = vec![];
373+
let mut new_labels = FxIndexSet::default();
374374

375375
for &sp in span.primary_spans() {
376376
if sp.is_dummy() {
@@ -387,7 +387,7 @@ pub trait Emitter: Translate {
387387
}
388388

389389
if always_backtrace {
390-
new_labels.push((
390+
new_labels.insert((
391391
trace.def_site,
392392
format!(
393393
"in this expansion of `{}`{}",
@@ -431,7 +431,7 @@ pub trait Emitter: Translate {
431431
format!("this {} desugaring", kind.descr()).into()
432432
}
433433
};
434-
new_labels.push((
434+
new_labels.insert((
435435
trace.call_site,
436436
format!(
437437
"in {}{}",

compiler/rustc_hir_typeck/src/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
650650
.sess
651651
.source_map()
652652
.is_multiline(call_expr.span.with_lo(callee_expr.span.hi()))
653-
&& call_expr.span.ctxt() == callee_expr.span.ctxt();
653+
&& call_expr.span.eq_ctxt(callee_expr.span);
654654
if call_is_multiline {
655655
err.span_suggestion(
656656
callee_expr.span.shrink_to_hi(),

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ lint_renamed_lint = lint `{$name}` has been renamed to `{$replace}`
494494
495495
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
496496
497+
lint_span_use_eq_ctxt = use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
498+
497499
lint_supertrait_as_deref_target = `{$t}` implements `Deref` with supertrait `{$target_principal}` as target
498500
.label = target type is set here
499501

compiler/rustc_lint/src/internal.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
44
use crate::lints::{
55
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistentDocKeyword,
6-
QueryInstability, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
6+
QueryInstability, SpanUseEqCtxtDiag, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
77
UntranslatableDiagnosticTrivial,
88
};
99
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
1010
use rustc_ast as ast;
1111
use rustc_hir::def::Res;
1212
use rustc_hir::{def_id::DefId, Expr, ExprKind, GenericArg, PatKind, Path, PathSegment, QPath};
13-
use rustc_hir::{HirId, Impl, Item, ItemKind, Node, Pat, Ty, TyKind};
13+
use rustc_hir::{BinOp, BinOpKind, HirId, Impl, Item, ItemKind, Node, Pat, Ty, TyKind};
1414
use rustc_middle::ty;
1515
use rustc_session::{declare_lint_pass, declare_tool_lint};
1616
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -537,3 +537,33 @@ impl LateLintPass<'_> for BadOptAccess {
537537
}
538538
}
539539
}
540+
541+
declare_tool_lint! {
542+
pub rustc::SPAN_USE_EQ_CTXT,
543+
Allow,
544+
"forbid uses of `==` with `Span::ctxt`, suggest `Span::eq_ctxt` instead",
545+
report_in_external_macro: true
546+
}
547+
548+
declare_lint_pass!(SpanUseEqCtxt => [SPAN_USE_EQ_CTXT]);
549+
550+
impl<'tcx> LateLintPass<'tcx> for SpanUseEqCtxt {
551+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) {
552+
if let ExprKind::Binary(BinOp { node: BinOpKind::Eq, .. }, lhs, rhs) = expr.kind {
553+
if is_span_ctxt_call(cx, lhs) && is_span_ctxt_call(cx, rhs) {
554+
cx.emit_spanned_lint(SPAN_USE_EQ_CTXT, expr.span, SpanUseEqCtxtDiag);
555+
}
556+
}
557+
}
558+
}
559+
560+
fn is_span_ctxt_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
561+
match &expr.kind {
562+
ExprKind::MethodCall(..) => cx
563+
.typeck_results()
564+
.type_dependent_def_id(expr.hir_id)
565+
.is_some_and(|call_did| cx.tcx.is_diagnostic_item(sym::SpanCtxt, call_did)),
566+
567+
_ => false,
568+
}
569+
}

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ fn register_internals(store: &mut LintStore) {
531531
store.register_late_mod_pass(|_| Box::new(BadOptAccess));
532532
store.register_lints(&PassByValue::get_lints());
533533
store.register_late_mod_pass(|_| Box::new(PassByValue));
534+
store.register_lints(&SpanUseEqCtxt::get_lints());
535+
store.register_late_mod_pass(|_| Box::new(SpanUseEqCtxt));
534536
// FIXME(davidtwco): deliberately do not include `UNTRANSLATABLE_DIAGNOSTIC` and
535537
// `DIAGNOSTIC_OUTSIDE_OF_IMPL` here because `-Wrustc::internal` is provided to every crate and
536538
// these lints will trigger all of the time - change this once migration to diagnostic structs
@@ -548,6 +550,7 @@ fn register_internals(store: &mut LintStore) {
548550
LintId::of(USAGE_OF_QUALIFIED_TY),
549551
LintId::of(EXISTING_DOC_KEYWORD),
550552
LintId::of(BAD_OPT_ACCESS),
553+
LintId::of(SPAN_USE_EQ_CTXT),
551554
],
552555
);
553556
}

compiler/rustc_lint/src/lints.rs

+4
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,10 @@ pub struct QueryInstability {
900900
pub query: Symbol,
901901
}
902902

903+
#[derive(LintDiagnostic)]
904+
#[diag(lint_span_use_eq_ctxt)]
905+
pub struct SpanUseEqCtxtDiag;
906+
903907
#[derive(LintDiagnostic)]
904908
#[diag(lint_tykind_kind)]
905909
pub struct TykindKind {

compiler/rustc_mir_transform/src/coverage/spans.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl<'a> CoverageSpansGenerator<'a> {
404404

405405
let Some(visible_macro) = curr.visible_macro(self.body_span) else { return };
406406
if let Some(prev) = &self.some_prev
407-
&& prev.expn_span.ctxt() == curr.expn_span.ctxt()
407+
&& prev.expn_span.eq_ctxt(curr.expn_span)
408408
{
409409
return;
410410
}

compiler/rustc_passes/src/loops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
231231
AsyncClosure(closure_span) => {
232232
self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
233233
}
234-
UnlabeledBlock(block_span) if is_break && block_span.ctxt() == break_span.ctxt() => {
234+
UnlabeledBlock(block_span) if is_break && block_span.eq_ctxt(break_span) => {
235235
let suggestion = Some(OutsideLoopSuggestion { block_span, break_span });
236236
self.sess.emit_err(OutsideLoop { span, name, is_break, suggestion });
237237
}

compiler/rustc_smir/src/rustc_internal/mod.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use crate::rustc_internal;
77
use crate::rustc_smir::Tables;
88
use rustc_data_structures::fx;
9+
use rustc_data_structures::fx::FxIndexMap;
910
use rustc_driver::{Callbacks, Compilation, RunCompiler};
1011
use rustc_interface::{interface, Queries};
1112
use rustc_middle::mir::interpret::AllocId;
13+
use rustc_middle::ty;
1214
use rustc_middle::ty::TyCtxt;
1315
use rustc_span::def_id::{CrateNum, DefId};
1416
use rustc_span::Span;
@@ -97,7 +99,7 @@ impl<'tcx> Tables<'tcx> {
9799
stable_mir::ty::Prov(self.create_alloc_id(aid))
98100
}
99101

100-
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
102+
pub(crate) fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
101103
self.def_ids.create_or_fetch(did)
102104
}
103105

@@ -108,6 +110,17 @@ impl<'tcx> Tables<'tcx> {
108110
pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
109111
self.spans.create_or_fetch(span)
110112
}
113+
114+
pub(crate) fn instance_def(
115+
&mut self,
116+
instance: ty::Instance<'tcx>,
117+
) -> stable_mir::mir::mono::InstanceDef {
118+
self.instances.create_or_fetch(instance)
119+
}
120+
121+
pub(crate) fn static_def(&mut self, did: DefId) -> stable_mir::mir::mono::StaticDef {
122+
stable_mir::mir::mono::StaticDef(self.create_def_id(did))
123+
}
111124
}
112125

113126
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
@@ -118,10 +131,11 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
118131
stable_mir::run(
119132
Tables {
120133
tcx,
121-
def_ids: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
122-
alloc_ids: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
123-
spans: rustc_internal::IndexMap { index_map: fx::FxIndexMap::default() },
134+
def_ids: IndexMap::default(),
135+
alloc_ids: IndexMap::default(),
136+
spans: IndexMap::default(),
124137
types: vec![],
138+
instances: IndexMap::default(),
125139
},
126140
f,
127141
);
@@ -192,6 +206,12 @@ pub struct IndexMap<K, V> {
192206
index_map: fx::FxIndexMap<K, V>,
193207
}
194208

209+
impl<K, V> Default for IndexMap<K, V> {
210+
fn default() -> Self {
211+
Self { index_map: FxIndexMap::default() }
212+
}
213+
}
214+
195215
impl<K: PartialEq + Hash + Eq, V: Copy + Debug + PartialEq + IndexedVal> IndexMap<K, V> {
196216
pub fn create_or_fetch(&mut self, key: K) -> V {
197217
let len = self.index_map.len();

compiler/rustc_smir/src/rustc_smir/mod.rs

+99-26
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
1313
use rustc_hir as hir;
1414
use rustc_middle::mir;
1515
use rustc_middle::mir::interpret::{alloc_range, AllocId};
16-
use rustc_middle::ty::{self, Ty, TyCtxt, Variance};
16+
use rustc_middle::mir::mono::MonoItem;
17+
use rustc_middle::ty::{self, Instance, ParamEnv, Ty, TyCtxt, Variance};
1718
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1819
use rustc_target::abi::FieldIdx;
19-
use stable_mir::mir::{CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
20+
use stable_mir::mir::mono::InstanceDef;
21+
use stable_mir::mir::{Body, CopyNonOverlapping, Statement, UserTypeProjection, VariantIdx};
2022
use stable_mir::ty::{
2123
FloatTy, GenericParamDef, IntTy, LineInfo, Movability, RigidTy, Span, TyKind, UintTy,
2224
};
@@ -119,29 +121,7 @@ impl<'tcx> Context for Tables<'tcx> {
119121

120122
fn mir_body(&mut self, item: stable_mir::DefId) -> stable_mir::mir::Body {
121123
let def_id = self[item];
122-
let mir = self.tcx.instance_mir(ty::InstanceDef::Item(def_id));
123-
stable_mir::mir::Body {
124-
blocks: mir
125-
.basic_blocks
126-
.iter()
127-
.map(|block| stable_mir::mir::BasicBlock {
128-
terminator: block.terminator().stable(self),
129-
statements: block
130-
.statements
131-
.iter()
132-
.map(|statement| statement.stable(self))
133-
.collect(),
134-
})
135-
.collect(),
136-
locals: mir
137-
.local_decls
138-
.iter()
139-
.map(|decl| stable_mir::mir::LocalDecl {
140-
ty: self.intern_ty(decl.ty),
141-
span: decl.source_info.span.stable(self),
142-
})
143-
.collect(),
144-
}
124+
self.tcx.instance_mir(ty::InstanceDef::Item(def_id)).stable(self)
145125
}
146126

147127
fn ty_kind(&mut self, ty: stable_mir::ty::Ty) -> TyKind {
@@ -190,6 +170,34 @@ impl<'tcx> Context for Tables<'tcx> {
190170
.collect(),
191171
}
192172
}
173+
174+
fn instance_body(&mut self, _def: InstanceDef) -> Body {
175+
todo!("Monomorphize the body")
176+
}
177+
178+
fn instance_ty(&mut self, def: InstanceDef) -> stable_mir::ty::Ty {
179+
let instance = self.instances[def];
180+
let ty = instance.ty(self.tcx, ParamEnv::empty());
181+
self.intern_ty(ty)
182+
}
183+
184+
fn instance_def_id(&mut self, def: InstanceDef) -> stable_mir::DefId {
185+
let def_id = self.instances[def].def_id();
186+
self.create_def_id(def_id)
187+
}
188+
189+
fn mono_instance(&mut self, item: stable_mir::CrateItem) -> stable_mir::mir::mono::Instance {
190+
let def_id = self[item.0];
191+
Instance::mono(self.tcx, def_id).stable(self)
192+
}
193+
194+
fn requires_monomorphization(&self, def_id: stable_mir::DefId) -> bool {
195+
let def_id = self[def_id];
196+
let generics = self.tcx.generics_of(def_id);
197+
let result = generics.requires_monomorphization(self.tcx);
198+
println!("req {result}: {def_id:?}");
199+
result
200+
}
193201
}
194202

195203
#[derive(Clone)]
@@ -224,7 +232,8 @@ pub struct Tables<'tcx> {
224232
pub def_ids: IndexMap<DefId, stable_mir::DefId>,
225233
pub alloc_ids: IndexMap<AllocId, stable_mir::AllocId>,
226234
pub spans: IndexMap<rustc_span::Span, Span>,
227-
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
235+
pub types: Vec<MaybeStable<TyKind, Ty<'tcx>>>,
236+
pub instances: IndexMap<ty::Instance<'tcx>, InstanceDef>,
228237
}
229238

230239
impl<'tcx> Tables<'tcx> {
@@ -254,6 +263,35 @@ pub(crate) trait Stable<'tcx> {
254263
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T;
255264
}
256265

266+
impl<'tcx> Stable<'tcx> for mir::Body<'tcx> {
267+
type T = stable_mir::mir::Body;
268+
269+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
270+
stable_mir::mir::Body {
271+
blocks: self
272+
.basic_blocks
273+
.iter()
274+
.map(|block| stable_mir::mir::BasicBlock {
275+
terminator: block.terminator().stable(tables),
276+
statements: block
277+
.statements
278+
.iter()
279+
.map(|statement| statement.stable(tables))
280+
.collect(),
281+
})
282+
.collect(),
283+
locals: self
284+
.local_decls
285+
.iter()
286+
.map(|decl| stable_mir::mir::LocalDecl {
287+
ty: tables.intern_ty(decl.ty),
288+
span: decl.source_info.span.stable(tables),
289+
})
290+
.collect(),
291+
}
292+
}
293+
}
294+
257295
impl<'tcx> Stable<'tcx> for mir::Statement<'tcx> {
258296
type T = stable_mir::mir::Statement;
259297
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
@@ -1637,3 +1675,38 @@ impl<'tcx> Stable<'tcx> for DefKind {
16371675
opaque(self)
16381676
}
16391677
}
1678+
1679+
impl<'tcx> Stable<'tcx> for ty::Instance<'tcx> {
1680+
type T = stable_mir::mir::mono::Instance;
1681+
1682+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1683+
let def = tables.instance_def(*self);
1684+
let kind = match self.def {
1685+
ty::InstanceDef::Item(..) => stable_mir::mir::mono::InstanceKind::Item,
1686+
ty::InstanceDef::Intrinsic(..) => stable_mir::mir::mono::InstanceKind::Intrinsic,
1687+
ty::InstanceDef::Virtual(..) => stable_mir::mir::mono::InstanceKind::Virtual,
1688+
ty::InstanceDef::VTableShim(..)
1689+
| ty::InstanceDef::ReifyShim(..)
1690+
| ty::InstanceDef::FnPtrAddrShim(..)
1691+
| ty::InstanceDef::ClosureOnceShim { .. }
1692+
| ty::InstanceDef::ThreadLocalShim(..)
1693+
| ty::InstanceDef::DropGlue(..)
1694+
| ty::InstanceDef::CloneShim(..)
1695+
| ty::InstanceDef::FnPtrShim(..) => stable_mir::mir::mono::InstanceKind::Shim,
1696+
};
1697+
stable_mir::mir::mono::Instance { def, kind }
1698+
}
1699+
}
1700+
1701+
impl<'tcx> Stable<'tcx> for MonoItem<'tcx> {
1702+
type T = stable_mir::mir::mono::MonoItem;
1703+
1704+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1705+
use stable_mir::mir::mono::MonoItem as StableMonoItem;
1706+
match self {
1707+
MonoItem::Fn(instance) => StableMonoItem::Fn(instance.stable(tables)),
1708+
MonoItem::Static(def_id) => StableMonoItem::Static(tables.static_def(*def_id)),
1709+
MonoItem::GlobalAsm(item_id) => StableMonoItem::GlobalAsm(opaque(item_id)),
1710+
}
1711+
}
1712+
}

0 commit comments

Comments
 (0)