From c3394b3eaa0f6fa135ddca67c3cabb3e9cd41a04 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 9 Mar 2023 22:05:57 +0800 Subject: Fix #107910, Shorten backtraces in ICEs --- compiler/rustc_query_impl/src/plumbing.rs | 93 +++++++++++++++++++------------ 1 file changed, 58 insertions(+), 35 deletions(-) (limited to 'compiler/rustc_query_impl/src') diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 5acfeb47a12..b17db0aa56a 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -488,6 +488,18 @@ macro_rules! expand_if_cached { }; } +/// Don't show the backtrace for query system by default +/// use `RUST_BACKTRACE=full` to show all the backtraces +#[inline(never)] +pub fn __rust_begin_short_backtrace(f: F) -> T +where + F: FnOnce() -> T, +{ + let result = f(); + std::hint::black_box(()); + result +} + // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros // invoked by `rustc_query_append`. macro_rules! define_queries { @@ -498,21 +510,25 @@ macro_rules! define_queries { use super::*; $( - #[inline(always)] - #[tracing::instrument(level = "trace", skip(tcx))] - pub(super) fn $name<'tcx>( - tcx: TyCtxt<'tcx>, - span: Span, - key: query_keys::$name<'tcx>, - mode: QueryMode, - ) -> Option>> { - get_query_incr( - queries::$name::config(tcx), - QueryCtxt::new(tcx), - span, - key, - mode - ) + // Adding `__rust_end_short_backtrace` marker to backtraces so that we emit the frames + // when `RUST_BACKTRACE=1`, add a new mod with `$name` here is to allow duplicate naming + pub mod $name { + use super::*; + #[inline(never)] + pub fn __rust_end_short_backtrace<'tcx>( + tcx: TyCtxt<'tcx>, + span: Span, + key: query_keys::$name<'tcx>, + mode: QueryMode, + ) -> Option>> { + get_query_incr( + queries::$name::config(tcx), + QueryCtxt::new(tcx), + span, + key, + mode + ) + } } )* } @@ -521,20 +537,22 @@ macro_rules! define_queries { use super::*; $( - #[inline(always)] - #[tracing::instrument(level = "trace", skip(tcx))] - pub(super) fn $name<'tcx>( - tcx: TyCtxt<'tcx>, - span: Span, - key: query_keys::$name<'tcx>, - __mode: QueryMode, - ) -> Option>> { - Some(get_query_non_incr( - queries::$name::config(tcx), - QueryCtxt::new(tcx), - span, - key, - )) + pub mod $name { + use super::*; + #[inline(never)] + pub fn __rust_end_short_backtrace<'tcx>( + tcx: TyCtxt<'tcx>, + span: Span, + key: query_keys::$name<'tcx>, + __mode: QueryMode, + ) -> Option>> { + Some(get_query_non_incr( + queries::$name::config(tcx), + QueryCtxt::new(tcx), + span, + key, + )) + } } )* } @@ -542,11 +560,11 @@ macro_rules! define_queries { pub(crate) fn engine(incremental: bool) -> QueryEngine { if incremental { QueryEngine { - $($name: get_query_incr::$name,)* + $($name: get_query_incr::$name::__rust_end_short_backtrace,)* } } else { QueryEngine { - $($name: get_query_non_incr::$name,)* + $($name: get_query_non_incr::$name::__rust_end_short_backtrace,)* } } } @@ -578,10 +596,15 @@ macro_rules! define_queries { query_cache: offset_of!(QueryCaches<'tcx> => $name), cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key), execute_query: |tcx, key| erase(tcx.$name(key)), - compute: |tcx, key| query_provided_to_value::$name( - tcx, - call_provider!([$($modifiers)*][tcx, $name, key]) - ), + compute: |tcx, key| { + use crate::plumbing::__rust_begin_short_backtrace; + __rust_begin_short_backtrace(|| + query_provided_to_value::$name( + tcx, + call_provider!([$($modifiers)*][tcx, $name, key]) + ) + ) + }, can_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] true false), try_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] { |tcx, key, prev_index, index| { -- cgit 1.4.1-3-g733a5