Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1945ce6

Browse files
committed
Auto merge of rust-lang#93922 - Mark-Simulacrum:beta-next, r=Mark-Simulacrum
[beta] backports This backports: * Complete removal of #[main] attribute from compiler rust-lang#93753 * Resolve lifetimes for const generic defaults rust-lang#93669 * backport llvm fix for issue 91671. rust-lang#93426 * Fix invalid special casing of the unreachable! macro rust-lang#93179 * Fix hashing for windows paths containing a CurDir component rust-lang#93697 r? `@Mark-Simulacrum`
2 parents f58f0df + 0ac18e7 commit 1945ce6

38 files changed

+434
-72
lines changed

compiler/rustc_builtin_macros/src/assert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::panic::use_panic_2021;
1+
use crate::edition_panic::use_panic_2021;
22
use rustc_ast::ptr::P;
33
use rustc_ast::token;
44
use rustc_ast::tokenstream::{DelimSpan, TokenStream};

compiler/rustc_builtin_macros/src/panic.rs renamed to compiler/rustc_builtin_macros/src/edition_panic.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,29 @@ pub fn expand_panic<'cx>(
2020
sp: Span,
2121
tts: TokenStream,
2222
) -> Box<dyn MacResult + 'cx> {
23-
let panic = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
23+
let mac = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
24+
expand(mac, cx, sp, tts)
25+
}
2426

27+
// This expands to either
28+
// - `$crate::panic::unreachable_2015!(...)` or
29+
// - `$crate::panic::unreachable_2021!(...)`
30+
// depending on the edition.
31+
pub fn expand_unreachable<'cx>(
32+
cx: &'cx mut ExtCtxt<'_>,
33+
sp: Span,
34+
tts: TokenStream,
35+
) -> Box<dyn MacResult + 'cx> {
36+
let mac = if use_panic_2021(sp) { sym::unreachable_2021 } else { sym::unreachable_2015 };
37+
expand(mac, cx, sp, tts)
38+
}
39+
40+
fn expand<'cx>(
41+
mac: rustc_span::Symbol,
42+
cx: &'cx mut ExtCtxt<'_>,
43+
sp: Span,
44+
tts: TokenStream,
45+
) -> Box<dyn MacResult + 'cx> {
2546
let sp = cx.with_call_site_ctxt(sp);
2647

2748
MacEager::expr(
@@ -31,7 +52,7 @@ pub fn expand_panic<'cx>(
3152
path: Path {
3253
span: sp,
3354
segments: cx
34-
.std_path(&[sym::panic, panic])
55+
.std_path(&[sym::panic, mac])
3556
.into_iter()
3657
.map(|ident| PathSegment::from_ident(ident))
3758
.collect(),

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ mod concat_bytes;
2929
mod concat_idents;
3030
mod derive;
3131
mod deriving;
32+
mod edition_panic;
3233
mod env;
3334
mod format;
3435
mod format_foreign;
3536
mod global_allocator;
3637
mod llvm_asm;
3738
mod log_syntax;
38-
mod panic;
3939
mod source_util;
4040
mod test;
4141
mod trace_macros;
@@ -82,8 +82,9 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
8282
log_syntax: log_syntax::expand_log_syntax,
8383
module_path: source_util::expand_mod,
8484
option_env: env::expand_option_env,
85-
core_panic: panic::expand_panic,
86-
std_panic: panic::expand_panic,
85+
core_panic: edition_panic::expand_panic,
86+
std_panic: edition_panic::expand_panic,
87+
unreachable: edition_panic::expand_unreachable,
8788
stringify: source_util::expand_stringify,
8889
trace_macros: trace_macros::expand_trace_macros,
8990
}

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
339339
),
340340

341341
// Entry point:
342-
ungated!(main, Normal, template!(Word), WarnFollowing),
343342
ungated!(start, Normal, template!(Word), WarnFollowing),
344343
ungated!(no_start, CrateLevel, template!(Word), WarnFollowing),
345344
ungated!(no_main, CrateLevel, template!(Word), WarnFollowing),

compiler/rustc_hir/src/lang_items.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ language_item_table! {
285285
Panic, sym::panic, panic_fn, Target::Fn, GenericRequirement::Exact(0);
286286
PanicFmt, sym::panic_fmt, panic_fmt, Target::Fn, GenericRequirement::None;
287287
PanicDisplay, sym::panic_display, panic_display, Target::Fn, GenericRequirement::None;
288-
PanicStr, sym::panic_str, panic_str, Target::Fn, GenericRequirement::None;
289288
ConstPanicFmt, sym::const_panic_fmt, const_panic_fmt, Target::Fn, GenericRequirement::None;
290289
PanicBoundsCheck, sym::panic_bounds_check, panic_bounds_check_fn, Target::Fn, GenericRequirement::Exact(0);
291290
PanicInfo, sym::panic_info, panic_info, Target::Struct, GenericRequirement::None;

compiler/rustc_lint/src/non_fmt_panic.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt {
4949
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
5050
if let hir::ExprKind::Call(f, [arg]) = &expr.kind {
5151
if let &ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(f).kind() {
52+
let f_diagnostic_name = cx.tcx.get_diagnostic_name(def_id);
53+
5254
if Some(def_id) == cx.tcx.lang_items().begin_panic_fn()
5355
|| Some(def_id) == cx.tcx.lang_items().panic_fn()
54-
|| Some(def_id) == cx.tcx.lang_items().panic_str()
56+
|| f_diagnostic_name == Some(sym::panic_str)
5557
{
5658
if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id {
5759
if matches!(
@@ -61,6 +63,22 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt {
6163
check_panic(cx, f, arg);
6264
}
6365
}
66+
} else if f_diagnostic_name == Some(sym::unreachable_display) {
67+
if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id {
68+
if cx.tcx.is_diagnostic_item(sym::unreachable_2015_macro, id) {
69+
check_panic(
70+
cx,
71+
f,
72+
// This is safe because we checked above that the callee is indeed
73+
// unreachable_display
74+
match &arg.kind {
75+
// Get the borrowed arg not the borrow
76+
hir::ExprKind::AddrOf(ast::BorrowKind::Ref, _, arg) => arg,
77+
_ => bug!("call to unreachable_display without borrow"),
78+
},
79+
);
80+
}
81+
}
6482
}
6583
}
6684
}
@@ -85,8 +103,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
85103
return;
86104
}
87105

88-
// Find the span of the argument to `panic!()`, before expansion in the
89-
// case of `panic!(some_macro!())`.
106+
// Find the span of the argument to `panic!()` or `unreachable!`, before expansion in the
107+
// case of `panic!(some_macro!())` or `unreachable!(some_macro!())`.
90108
// We don't use source_callsite(), because this `panic!(..)` might itself
91109
// be expanded from another macro, in which case we want to stop at that
92110
// expansion.
@@ -319,6 +337,7 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span,
319337
| sym::std_panic_macro
320338
| sym::assert_macro
321339
| sym::debug_assert_macro
340+
| sym::unreachable_macro
322341
) {
323342
break;
324343
}

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1350,11 +1350,14 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13501350
this.visit_ty(&ty);
13511351
}
13521352
}
1353-
GenericParamKind::Const { ref ty, .. } => {
1353+
GenericParamKind::Const { ref ty, default } => {
13541354
let was_in_const_generic = this.is_in_const_generic;
13551355
this.is_in_const_generic = true;
13561356
walk_list!(this, visit_param_bound, param.bounds);
13571357
this.visit_ty(&ty);
1358+
if let Some(default) = default {
1359+
this.visit_body(this.tcx.hir().body(default.body));
1360+
}
13581361
this.is_in_const_generic = was_in_const_generic;
13591362
}
13601363
}

compiler/rustc_span/src/symbol.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,13 @@ symbols! {
13881388
unmarked_api,
13891389
unpin,
13901390
unreachable,
1391+
unreachable_2015,
1392+
unreachable_2015_macro,
1393+
unreachable_2021,
1394+
unreachable_2021_macro,
13911395
unreachable_code,
1396+
unreachable_display,
1397+
unreachable_macro,
13921398
unrestricted_attribute_tokens,
13931399
unsafe_block_in_unsafe_fn,
13941400
unsafe_cell,

library/core/src/macros/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,22 @@ macro_rules! writeln {
587587
/// unreachable!("The loop should always return");
588588
/// }
589589
/// ```
590+
#[cfg(not(bootstrap))]
591+
#[macro_export]
592+
#[rustc_builtin_macro(unreachable)]
593+
#[allow_internal_unstable(edition_panic)]
594+
#[stable(feature = "rust1", since = "1.0.0")]
595+
#[cfg_attr(not(test), rustc_diagnostic_item = "unreachable_macro")]
596+
macro_rules! unreachable {
597+
// Expands to either `$crate::panic::unreachable_2015` or `$crate::panic::unreachable_2021`
598+
// depending on the edition of the caller.
599+
($($arg:tt)*) => {
600+
/* compiler built-in */
601+
};
602+
}
603+
604+
/// unreachable!() macro
605+
#[cfg(bootstrap)]
590606
#[macro_export]
591607
#[stable(feature = "rust1", since = "1.0.0")]
592608
#[allow_internal_unstable(core_panic)]

library/core/src/panic.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,39 @@ pub macro panic_2021 {
5858
),
5959
}
6060

61+
#[doc(hidden)]
62+
#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
63+
#[allow_internal_unstable(core_panic)]
64+
#[rustc_diagnostic_item = "unreachable_2015_macro"]
65+
#[rustc_macro_transparency = "semitransparent"]
66+
pub macro unreachable_2015 {
67+
() => (
68+
$crate::panicking::panic("internal error: entered unreachable code")
69+
),
70+
// Use of `unreachable_display` for non_fmt_panic lint.
71+
// NOTE: the message ("internal error ...") is embeded directly in unreachable_display
72+
($msg:expr $(,)?) => (
73+
$crate::panicking::unreachable_display(&$msg)
74+
),
75+
($fmt:expr, $($arg:tt)*) => (
76+
$crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
77+
),
78+
}
79+
80+
#[doc(hidden)]
81+
#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
82+
#[allow_internal_unstable(core_panic)]
83+
#[rustc_diagnostic_item = "unreachable_2021_macro"]
84+
#[rustc_macro_transparency = "semitransparent"]
85+
pub macro unreachable_2021 {
86+
() => (
87+
$crate::panicking::panic("internal error: entered unreachable code")
88+
),
89+
($($t:tt)+) => (
90+
$crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+))
91+
),
92+
}
93+
6194
/// An internal trait used by libstd to pass data from libstd to `panic_unwind`
6295
/// and other panic runtimes. Not intended to be stabilized any time soon, do
6396
/// not use.

0 commit comments

Comments
 (0)