Skip to content

Commit 2e02105

Browse files
committed
Auto merge of #153059 - GuillaumeGomez:rollup-bobduJ9, r=GuillaumeGomez
Rollup of 8 pull requests Successful merges: - #149169 (ptr::replace: make calls on ZST null ptr not UB) - #150562 (Fix doc link used in suggestion for pinning self) - #152679 (rustc_expand: improve diagnostics for non-repeatable metavars) - #153017 (Implement debuginfo for unsafe binder types) - #152868 (delete some very old trivial `Box` tests) - #152922 (rustc_public: Make fields that shouldn't be exposed visible only in `rustc_public`) - #153029 (Rename `rustc::pass_by_value` lint as `rustc::disallowed_pass_by_ref`.) - #153051 (Migration of `LintDiagnostic` - part 3)
2 parents 859951e + fea905f commit 2e02105

File tree

89 files changed

+751
-497
lines changed

Some content is hidden

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

89 files changed

+751
-497
lines changed

compiler/rustc_ast/src/visit.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,11 @@ macro_rules! common_visitor_and_walkers {
757757
) -> V::Result;
758758
}
759759

760-
// this is only used by the MutVisitor. We include this symmetry here to make writing other functions easier
760+
// This is only used by the MutVisitor. We include this symmetry here to make writing other
761+
// functions easier.
761762
$(${ignore($lt)}
762-
#[expect(unused, rustc::pass_by_value)]
763+
#[cfg_attr(not(bootstrap), expect(unused, rustc::disallowed_pass_by_ref))]
764+
#[cfg_attr(bootstrap, expect(unused, rustc::pass_by_value))]
763765
#[inline]
764766
)?
765767
fn visit_span<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, span: &$($lt)? $($mut)? Span) -> V::Result {

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,12 +2698,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
26982698

26992699
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
27002700

2701-
tcx.emit_diag_node_span_lint(
2702-
UNUSED_MUT,
2703-
lint_root,
2704-
span,
2705-
VarNeedNotMut { span: mut_span },
2706-
)
2701+
tcx.emit_node_span_lint(UNUSED_MUT, lint_root, span, VarNeedNotMut { span: mut_span })
27072702
}
27082703
}
27092704
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,7 @@ pub(crate) fn spanned_type_di_node<'ll, 'tcx>(
480480
},
481481
ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
482482
ty::Pat(base, _) => return type_di_node(cx, base),
483-
// FIXME(unsafe_binders): impl debug info
484-
ty::UnsafeBinder(_) => unimplemented!(),
483+
ty::UnsafeBinder(_) => build_unsafe_binder_type_di_node(cx, t, unique_type_id),
485484
ty::Alias(..)
486485
| ty::Param(_)
487486
| ty::Bound(..)
@@ -1488,6 +1487,56 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
14881487
.di_node
14891488
}
14901489

1490+
/// Creates the debuginfo node for `unsafe<'a> T` binder types.
1491+
///
1492+
/// We treat an unsafe binder like a struct with a single field named `inner`
1493+
/// rather than delegating to the inner type's DI node directly. This way the
1494+
/// debugger shows the binder's own type name, and the wrapped value is still
1495+
/// accessible through the `inner` field.
1496+
fn build_unsafe_binder_type_di_node<'ll, 'tcx>(
1497+
cx: &CodegenCx<'ll, 'tcx>,
1498+
binder_type: Ty<'tcx>,
1499+
unique_type_id: UniqueTypeId<'tcx>,
1500+
) -> DINodeCreationResult<'ll> {
1501+
let ty::UnsafeBinder(inner) = binder_type.kind() else {
1502+
bug!(
1503+
"Only ty::UnsafeBinder is valid for build_unsafe_binder_type_di_node. Found {:?} instead.",
1504+
binder_type
1505+
)
1506+
};
1507+
let inner_type = inner.skip_binder();
1508+
let inner_type_di_node = type_di_node(cx, inner_type);
1509+
1510+
let type_name = compute_debuginfo_type_name(cx.tcx, binder_type, true);
1511+
type_map::build_type_with_children(
1512+
cx,
1513+
type_map::stub(
1514+
cx,
1515+
Stub::Struct,
1516+
unique_type_id,
1517+
&type_name,
1518+
None,
1519+
cx.size_and_align_of(binder_type),
1520+
NO_SCOPE_METADATA,
1521+
DIFlags::FlagZero,
1522+
),
1523+
|cx, unsafe_binder_type_di_node| {
1524+
let inner_layout = cx.layout_of(inner_type);
1525+
smallvec![build_field_di_node(
1526+
cx,
1527+
unsafe_binder_type_di_node,
1528+
"inner",
1529+
inner_layout,
1530+
Size::ZERO,
1531+
DIFlags::FlagZero,
1532+
inner_type_di_node,
1533+
None,
1534+
)]
1535+
},
1536+
NO_GENERICS,
1537+
)
1538+
}
1539+
14911540
/// Get the global variable for the vtable.
14921541
///
14931542
/// When using global variables, we may have created an addrspacecast to get a pointer to the

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,19 @@ fn push_debuginfo_type_name<'tcx>(
430430
push_closure_or_coroutine_name(tcx, def_id, args, qualified, output, visited);
431431
}
432432
}
433-
ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binders)"),
433+
ty::UnsafeBinder(inner) => {
434+
if cpp_like_debuginfo {
435+
output.push_str("unsafe$<");
436+
} else {
437+
output.push_str("unsafe ");
438+
}
439+
440+
push_debuginfo_type_name(tcx, inner.skip_binder(), qualified, output, visited);
441+
442+
if cpp_like_debuginfo {
443+
push_close_angle_bracket(cpp_like_debuginfo, output);
444+
}
445+
}
434446
ty::Param(_)
435447
| ty::Error(_)
436448
| ty::Infer(_)

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) fn from_target_feature_attr(
7575
// For "neon" specifically, we emit an FCW instead of a hard error.
7676
// See <https://github.com/rust-lang/rust/issues/134375>.
7777
if tcx.sess.target.arch == Arch::AArch64 && name.as_str() == "neon" {
78-
tcx.emit_diag_node_span_lint(
78+
tcx.emit_node_span_lint(
7979
AARCH64_SOFTFLOAT_NEON,
8080
tcx.local_def_id_to_hir_id(did),
8181
feature_span,

compiler/rustc_const_eval/src/const_eval/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,5 @@ pub(super) fn lint<'tcx, L>(
256256
{
257257
let (span, frames) = get_span_and_frames(tcx, &machine.stack);
258258

259-
tcx.emit_diag_node_span_lint(lint, machine.best_lint_scope(*tcx), span, decorator(frames));
259+
tcx.emit_node_span_lint(lint, machine.best_lint_scope(*tcx), span, decorator(frames));
260260
}

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
718718
.level
719719
.is_error();
720720
let span = ecx.cur_span();
721-
ecx.tcx.emit_diag_node_span_lint(
721+
ecx.tcx.emit_node_span_lint(
722722
rustc_session::lint::builtin::LONG_RUNNING_CONST_EVAL,
723723
hir_id,
724724
span,

compiler/rustc_expand/src/base.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,16 @@ impl<'cx> MacroExpanderResult<'cx> {
277277
// Emit the SEMICOLON_IN_EXPRESSIONS_FROM_MACROS deprecation lint.
278278
let is_local = true;
279279

280-
let parser = ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, is_local, macro_ident);
280+
let parser = ParserAnyMacro::from_tts(
281+
cx,
282+
tts,
283+
site_span,
284+
arm_span,
285+
is_local,
286+
macro_ident,
287+
vec![],
288+
vec![],
289+
);
281290
ExpandResult::Ready(Box::new(parser))
282291
}
283292
}

compiler/rustc_expand/src/mbe/diagnostics.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,13 @@ impl<'dcx> CollectTrackerAndEmitter<'dcx, '_> {
222222

223223
pub(super) fn emit_frag_parse_err(
224224
mut e: Diag<'_>,
225-
parser: &Parser<'_>,
225+
parser: &mut Parser<'_>,
226226
orig_parser: &mut Parser<'_>,
227227
site_span: Span,
228228
arm_span: Span,
229229
kind: AstFragmentKind,
230+
bindings: Vec<Ident>,
231+
matched_rule_bindings: Vec<Ident>,
230232
) -> ErrorGuaranteed {
231233
// FIXME(davidtwco): avoid depending on the error message text
232234
if parser.token == token::Eof
@@ -285,6 +287,54 @@ pub(super) fn emit_frag_parse_err(
285287
},
286288
_ => annotate_err_with_kind(&mut e, kind, site_span),
287289
};
290+
291+
let matched_rule_bindings_names: Vec<_> =
292+
matched_rule_bindings.iter().map(|bind| bind.name).collect();
293+
let bindings_name: Vec<_> = bindings.iter().map(|bind| bind.name).collect();
294+
if parser.token.kind == token::Dollar {
295+
parser.bump();
296+
if let token::Ident(name, _) = parser.token.kind {
297+
if let Some(matched_name) = rustc_span::edit_distance::find_best_match_for_name(
298+
&matched_rule_bindings_names[..],
299+
name,
300+
None,
301+
) {
302+
e.span_suggestion_verbose(
303+
parser.token.span,
304+
"there is a macro metavariable with similar name",
305+
format!("{matched_name}"),
306+
Applicability::MaybeIncorrect,
307+
);
308+
} else if bindings_name.contains(&name) {
309+
e.span_label(
310+
parser.token.span,
311+
format!(
312+
"there is an macro metavariable with this name in another macro matcher"
313+
),
314+
);
315+
} else if let Some(matched_name) =
316+
rustc_span::edit_distance::find_best_match_for_name(&bindings_name[..], name, None)
317+
{
318+
e.span_suggestion_verbose(
319+
parser.token.span,
320+
"there is a macro metavariable with a similar name in another macro matcher",
321+
format!("{matched_name}"),
322+
Applicability::MaybeIncorrect,
323+
);
324+
} else {
325+
let msg = matched_rule_bindings_names
326+
.iter()
327+
.map(|sym| format!("${}", sym))
328+
.collect::<Vec<_>>()
329+
.join(", ");
330+
331+
e.span_label(parser.token.span, format!("macro metavariable not found"));
332+
if !matched_rule_bindings_names.is_empty() {
333+
e.note(format!("available metavariable names are: {msg}"));
334+
}
335+
}
336+
}
337+
}
288338
e.emit()
289339
}
290340

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub(crate) struct ParserAnyMacro<'a> {
5555
arm_span: Span,
5656
/// Whether or not this macro is defined in the current crate
5757
is_local: bool,
58+
bindings: Vec<Ident>,
59+
matched_rule_bindings: Vec<Ident>,
5860
}
5961

6062
impl<'a> ParserAnyMacro<'a> {
@@ -67,13 +69,22 @@ impl<'a> ParserAnyMacro<'a> {
6769
arm_span,
6870
is_trailing_mac,
6971
is_local,
72+
bindings,
73+
matched_rule_bindings,
7074
} = *self;
7175
let snapshot = &mut parser.create_snapshot_for_diagnostic();
7276
let fragment = match parse_ast_fragment(parser, kind) {
7377
Ok(f) => f,
7478
Err(err) => {
7579
let guar = diagnostics::emit_frag_parse_err(
76-
err, parser, snapshot, site_span, arm_span, kind,
80+
err,
81+
parser,
82+
snapshot,
83+
site_span,
84+
arm_span,
85+
kind,
86+
bindings,
87+
matched_rule_bindings,
7788
);
7889
return kind.dummy(site_span, guar);
7990
}
@@ -108,6 +119,9 @@ impl<'a> ParserAnyMacro<'a> {
108119
arm_span: Span,
109120
is_local: bool,
110121
macro_ident: Ident,
122+
// bindings and lhs is for diagnostics
123+
bindings: Vec<Ident>,
124+
matched_rule_bindings: Vec<Ident>,
111125
) -> Self {
112126
Self {
113127
parser: Parser::new(&cx.sess.psess, tts, None),
@@ -121,6 +135,8 @@ impl<'a> ParserAnyMacro<'a> {
121135
is_trailing_mac: cx.current_expansion.is_trailing_mac,
122136
arm_span,
123137
is_local,
138+
bindings,
139+
matched_rule_bindings,
124140
}
125141
}
126142
}
@@ -359,7 +375,7 @@ fn expand_macro<'cx>(
359375

360376
match try_success_result {
361377
Ok((rule_index, rule, named_matches)) => {
362-
let MacroRule::Func { rhs, .. } = rule else {
378+
let MacroRule::Func { lhs, rhs, .. } = rule else {
363379
panic!("try_match_macro returned non-func rule");
364380
};
365381
let mbe::TokenTree::Delimited(rhs_span, _, rhs) = rhs else {
@@ -387,8 +403,32 @@ fn expand_macro<'cx>(
387403
cx.resolver.record_macro_rule_usage(node_id, rule_index);
388404
}
389405

406+
let mut bindings = vec![];
407+
for rule in rules {
408+
let MacroRule::Func { lhs, .. } = rule else { continue };
409+
for param in lhs {
410+
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
411+
bindings.push(*bind);
412+
}
413+
}
414+
415+
let mut matched_rule_bindings = vec![];
416+
for param in lhs {
417+
let MatcherLoc::MetaVarDecl { bind, .. } = param else { continue };
418+
matched_rule_bindings.push(*bind);
419+
}
420+
390421
// Let the context choose how to interpret the result. Weird, but useful for X-macros.
391-
Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name))
422+
Box::new(ParserAnyMacro::from_tts(
423+
cx,
424+
tts,
425+
sp,
426+
arm_span,
427+
is_local,
428+
name,
429+
bindings,
430+
matched_rule_bindings,
431+
))
392432
}
393433
Err(CanRetry::No(guar)) => {
394434
debug!("Will not retry matching as an error was emitted already");

0 commit comments

Comments
 (0)