Skip to content

Commit 0d05bf8

Browse files
committed
Improved collapse_debuginfo attribute, added command-line flag (no|external|yes)
1 parent be00c5a commit 0d05bf8

File tree

10 files changed

+150
-21
lines changed

10 files changed

+150
-21
lines changed

compiler/rustc_expand/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ expand_attributes_wrong_form =
1616
expand_cannot_be_name_of_macro =
1717
`{$trait_ident}` cannot be a name of {$macro_type} macro
1818
19+
expand_collapse_debuginfo_illegal =
20+
illegal value {$sym} for attribute #[collapse_debuginfo(no|external|yes)]
21+
1922
expand_count_repetition_misplaced =
2023
`count` can not be placed inside the inner-most repetition
2124

compiler/rustc_expand/src/base.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_session::errors::report_lit_error;
2323
use rustc_session::{parse::ParseSess, Limit, Session};
2424
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
2525
use rustc_span::edition::Edition;
26-
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
26+
use rustc_span::hygiene::{AstPass, CollapseDebuginfo, ExpnData, ExpnKind, LocalExpnId};
2727
use rustc_span::source_map::SourceMap;
2828
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2929
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
@@ -727,7 +727,7 @@ pub struct SyntaxExtension {
727727
pub local_inner_macros: bool,
728728
/// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
729729
/// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
730-
pub collapse_debuginfo: bool,
730+
pub collapse_debuginfo: CollapseDebuginfo,
731731
}
732732

733733
impl SyntaxExtension {
@@ -757,7 +757,26 @@ impl SyntaxExtension {
757757
kind,
758758
allow_internal_unsafe: false,
759759
local_inner_macros: false,
760-
collapse_debuginfo: false,
760+
collapse_debuginfo: CollapseDebuginfo::Unspecified,
761+
}
762+
}
763+
764+
fn collapse_debuginfo_by_name(sess: &Session, attr: &Attribute) -> CollapseDebuginfo {
765+
if let Some(value) = attr.value_str() {
766+
match value {
767+
sym::no => CollapseDebuginfo::No,
768+
sym::external => CollapseDebuginfo::External,
769+
sym::yes => CollapseDebuginfo::Yes,
770+
_ => {
771+
sess.dcx()
772+
.emit_err(errors::CollapseDebuginfoIllegal { span: attr.span, sym: value });
773+
CollapseDebuginfo::Unspecified
774+
}
775+
}
776+
} else {
777+
// #[collapse_debuginfo] without enum value (#[collapse_debuginfo(no/external/yes)])
778+
// considered as `yes`
779+
CollapseDebuginfo::Yes
761780
}
762781
}
763782

@@ -780,7 +799,9 @@ impl SyntaxExtension {
780799
let local_inner_macros = attr::find_by_name(attrs, sym::macro_export)
781800
.and_then(|macro_export| macro_export.meta_item_list())
782801
.is_some_and(|l| attr::list_contains_name(&l, sym::local_inner_macros));
783-
let collapse_debuginfo = attr::contains_name(attrs, sym::collapse_debuginfo);
802+
let collapse_debuginfo = attr::find_by_name(attrs, sym::collapse_debuginfo)
803+
.and_then(|v| Some(Self::collapse_debuginfo_by_name(sess, v)))
804+
.unwrap_or(CollapseDebuginfo::Unspecified);
784805
tracing::debug!(?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
785806

786807
let (builtin_name, helper_attrs) = attr::find_by_name(attrs, sym::rustc_builtin_macro)

compiler/rustc_expand/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ pub(crate) struct ResolveRelativePath {
5858
pub path: String,
5959
}
6060

61+
#[derive(Diagnostic)]
62+
#[diag(expand_collapse_debuginfo_illegal)]
63+
pub(crate) struct CollapseDebuginfoIllegal {
64+
#[primary_span]
65+
pub span: Span,
66+
pub sym: Symbol,
67+
}
68+
6169
#[derive(Diagnostic)]
6270
#[diag(expand_macro_const_stability)]
6371
pub(crate) struct MacroConstStability {

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
469469

470470
// `#[collapse_debuginfo]`
471471
gated!(
472-
collapse_debuginfo, Normal, template!(Word), WarnFollowing,
472+
collapse_debuginfo, Normal, template!(NameValueStr: "no|external|yes|..."), WarnFollowing,
473473
experimental!(collapse_debuginfo)
474474
),
475475

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ fn test_unstable_options_tracking_hash() {
742742
})
743743
);
744744
tracked!(codegen_backend, Some("abc".to_string()));
745+
tracked!(collapse_debuginfo, CollapseDebuginfo::Yes);
745746
tracked!(crate_attr, vec!["abc".to_string()]);
746747
tracked!(cross_crate_inline_threshold, InliningThreshold::Always);
747748
tracked!(debug_info_for_profiling, true);

compiler/rustc_middle/src/ty/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use rustc_query_system::ich::StableHashingContext;
4949
use rustc_serialize::{Decodable, Encodable};
5050
use rustc_session::lint::LintBuffer;
5151
pub use rustc_session::lint::RegisteredTools;
52-
use rustc_span::hygiene::MacroKind;
52+
use rustc_span::hygiene::{CollapseDebuginfo, MacroKind};
5353
use rustc_span::symbol::{kw, sym, Ident, Symbol};
5454
use rustc_span::{hygiene, ExpnId, ExpnKind, Span};
5555
use rustc_target::abi::{Align, FieldIdx, Integer, IntegerType, VariantIdx};
@@ -2531,8 +2531,18 @@ impl<'tcx> TyCtxt<'tcx> {
25312531
if self.sess.opts.unstable_opts.debug_macros || !span.from_expansion() {
25322532
return span;
25332533
}
2534-
let collapse_debuginfo_enabled = self.features().collapse_debuginfo;
2535-
hygiene::walk_chain_collapsed(span, upto, collapse_debuginfo_enabled)
2534+
let collapse_debuginfo_flag = match self.sess.opts.unstable_opts.collapse_debuginfo {
2535+
rustc_session::config::CollapseDebuginfo::Unspecified => CollapseDebuginfo::Unspecified,
2536+
rustc_session::config::CollapseDebuginfo::No => CollapseDebuginfo::No,
2537+
rustc_session::config::CollapseDebuginfo::External => CollapseDebuginfo::External,
2538+
rustc_session::config::CollapseDebuginfo::Yes => CollapseDebuginfo::Yes,
2539+
};
2540+
hygiene::walk_chain_collapsed(
2541+
span,
2542+
upto,
2543+
self.features().collapse_debuginfo,
2544+
collapse_debuginfo_flag,
2545+
)
25362546
}
25372547

25382548
#[inline]

compiler/rustc_session/src/config.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -3202,12 +3202,12 @@ pub enum WasiExecModel {
32023202
/// how the hash should be calculated when adding a new command-line argument.
32033203
pub(crate) mod dep_tracking {
32043204
use super::{
3205-
BranchProtection, CFGuard, CFProtection, CrateType, DebugInfo, DebugInfoCompression,
3206-
ErrorOutputType, FunctionReturn, InliningThreshold, InstrumentCoverage, InstrumentXRay,
3207-
LinkerPluginLto, LocationDetail, LtoCli, NextSolverConfig, OomStrategy, OptLevel,
3208-
OutFileName, OutputType, OutputTypes, Polonius, RemapPathScopeComponents, ResolveDocLinks,
3209-
SourceFileHashAlgorithm, SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion,
3210-
TrimmedDefPaths, WasiExecModel,
3205+
BranchProtection, CFGuard, CFProtection, CollapseDebuginfo, CrateType, DebugInfo,
3206+
DebugInfoCompression, ErrorOutputType, FunctionReturn, InliningThreshold,
3207+
InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail, LtoCli,
3208+
NextSolverConfig, OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes, Polonius,
3209+
RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm, SplitDwarfKind,
3210+
SwitchWithOptPath, SymbolManglingVersion, TrimmedDefPaths, WasiExecModel,
32113211
};
32123212
use crate::lint;
32133213
use crate::utils::NativeLib;
@@ -3286,6 +3286,7 @@ pub(crate) mod dep_tracking {
32863286
LtoCli,
32873287
DebugInfo,
32883288
DebugInfoCompression,
3289+
CollapseDebuginfo,
32893290
UnstableFeatures,
32903291
NativeLib,
32913292
SanitizerSet,
@@ -3450,6 +3451,25 @@ pub enum ProcMacroExecutionStrategy {
34503451
CrossThread,
34513452
}
34523453

3454+
/// How to perform collapse macros debug info
3455+
/// if-ext - if macro from different crate (related to callsite code)
3456+
/// | cmd \ attr | no | (unspecified) | external | yes |
3457+
/// | no | no | no | no | no |
3458+
/// | (unspecified) | no | no | if-ext | yes |
3459+
/// | external | no | if-ext | if-ext | yes |
3460+
/// | yes | yes | yes | yes | yes |
3461+
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
3462+
pub enum CollapseDebuginfo {
3463+
/// Unspecified value
3464+
Unspecified = 0,
3465+
/// Don't collapse debuginfo for the macro
3466+
No = 1,
3467+
/// Collapse debuginfo if command line flag enables collapsing
3468+
External = 2,
3469+
/// Collapse debuginfo for the macro
3470+
Yes = 3,
3471+
}
3472+
34533473
/// Which format to use for `-Z dump-mono-stats`
34543474
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
34553475
pub enum DumpMonoStatsFormat {

compiler/rustc_session/src/options.rs

+13
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ mod desc {
388388
pub const parse_cfprotection: &str = "`none`|`no`|`n` (default), `branch`, `return`, or `full`|`yes`|`y` (equivalent to `branch` and `return`)";
389389
pub const parse_debuginfo: &str = "either an integer (0, 1, 2), `none`, `line-directives-only`, `line-tables-only`, `limited`, or `full`";
390390
pub const parse_debuginfo_compression: &str = "one of `none`, `zlib`, or `zstd`";
391+
pub const parse_collapse_debuginfo: &str = "one of `no`, `external`, or `yes`";
391392
pub const parse_strip: &str = "either `none`, `debuginfo`, or `symbols`";
392393
pub const parse_linker_flavor: &str = ::rustc_target::spec::LinkerFlavorCli::one_of();
393394
pub const parse_optimization_fuel: &str = "crate=integer";
@@ -1302,6 +1303,16 @@ mod parse {
13021303
true
13031304
}
13041305

1306+
pub(crate) fn parse_collapse_debuginfo(slot: &mut CollapseDebuginfo, v: Option<&str>) -> bool {
1307+
*slot = match v {
1308+
Some("no") => CollapseDebuginfo::No,
1309+
Some("external") => CollapseDebuginfo::External,
1310+
Some("yes") => CollapseDebuginfo::Yes,
1311+
_ => return false,
1312+
};
1313+
true
1314+
}
1315+
13051316
pub(crate) fn parse_proc_macro_execution_strategy(
13061317
slot: &mut ProcMacroExecutionStrategy,
13071318
v: Option<&str>,
@@ -1534,6 +1545,8 @@ options! {
15341545
"instrument control-flow architecture protection"),
15351546
codegen_backend: Option<String> = (None, parse_opt_string, [TRACKED],
15361547
"the backend to use"),
1548+
collapse_debuginfo: CollapseDebuginfo = (CollapseDebuginfo::Unspecified, parse_collapse_debuginfo, [TRACKED],
1549+
"set option for collapse macros debug info"),
15371550
combine_cgu: bool = (false, parse_bool, [TRACKED],
15381551
"combine CGUs into a single one"),
15391552
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],

compiler/rustc_span/src/hygiene.rs

+58-7
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,41 @@ pub enum Transparency {
163163
Opaque,
164164
}
165165

166+
/// How to perform collapse macros debug info
167+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, Encodable, Decodable)]
168+
#[derive(HashStable_Generic)]
169+
pub enum CollapseDebuginfo {
170+
/// Unspecified value
171+
Unspecified = 0,
172+
/// Don't collapse debuginfo for the macro
173+
No = 1,
174+
/// Collapse debuginfo if command line flag enables collapsing
175+
External = 2,
176+
/// Collapse debuginfo for the macro
177+
Yes = 3,
178+
}
179+
180+
/// if-ext - if macro from different crate (related to callsite code)
181+
/// | cmd \ attr | no | (unspecified) | external | yes |
182+
/// | no | no | no | no | no |
183+
/// | (unspecified) | no | no | if-ext | yes |
184+
/// | external | no | if-ext | if-ext | yes |
185+
/// | yes | yes | yes | yes | yes |
186+
impl CollapseDebuginfo {
187+
pub fn should_collapse(self, flag: CollapseDebuginfo, ext: bool) -> bool {
188+
const NO: bool = false;
189+
const YES: bool = true;
190+
#[rustfmt::skip]
191+
let collapse_table = [
192+
[NO, NO, NO, NO ],
193+
[NO, NO, ext, YES],
194+
[NO, ext, ext, YES],
195+
[YES, YES, YES, YES],
196+
];
197+
collapse_table[flag as usize][self as usize]
198+
}
199+
}
200+
166201
impl LocalExpnId {
167202
/// The ID of the theoretical expansion that generates freshly parsed, unexpanded AST.
168203
pub const ROOT: LocalExpnId = LocalExpnId::from_u32(0);
@@ -464,7 +499,8 @@ impl HygieneData {
464499
&self,
465500
mut span: Span,
466501
to: Span,
467-
collapse_debuginfo_enabled: bool,
502+
collapse_debuginfo_feature_enabled: bool,
503+
collapse_debuginfo_flag: CollapseDebuginfo,
468504
) -> Span {
469505
let orig_span = span;
470506
let mut ret_span = span;
@@ -477,7 +513,10 @@ impl HygieneData {
477513
let expn_data = self.expn_data(outer_expn);
478514
debug!("walk_chain_collapsed({:?}): expn_data={:?}", span, expn_data);
479515
span = expn_data.call_site;
480-
if !collapse_debuginfo_enabled || expn_data.collapse_debuginfo {
516+
let is_ext = !expn_data.macro_def_id.map_or(false, |v| v.is_local());
517+
if !collapse_debuginfo_feature_enabled
518+
|| expn_data.collapse_debuginfo.should_collapse(collapse_debuginfo_flag, is_ext)
519+
{
481520
ret_span = span;
482521
}
483522
}
@@ -601,8 +640,20 @@ pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
601640
HygieneData::with(|data| data.walk_chain(span, to))
602641
}
603642

604-
pub fn walk_chain_collapsed(span: Span, to: Span, collapse_debuginfo_enabled: bool) -> Span {
605-
HygieneData::with(|hdata| hdata.walk_chain_collapsed(span, to, collapse_debuginfo_enabled))
643+
pub fn walk_chain_collapsed(
644+
span: Span,
645+
to: Span,
646+
collapse_debuginfo_feature_enabled: bool,
647+
collapse_debuginfo_flag: CollapseDebuginfo,
648+
) -> Span {
649+
HygieneData::with(|hdata| {
650+
hdata.walk_chain_collapsed(
651+
span,
652+
to,
653+
collapse_debuginfo_feature_enabled,
654+
collapse_debuginfo_flag,
655+
)
656+
})
606657
}
607658

608659
pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
@@ -957,7 +1008,7 @@ pub struct ExpnData {
9571008
pub local_inner_macros: bool,
9581009
/// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
9591010
/// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
960-
pub(crate) collapse_debuginfo: bool,
1011+
pub(crate) collapse_debuginfo: CollapseDebuginfo,
9611012
}
9621013

9631014
impl !PartialEq for ExpnData {}
@@ -975,7 +1026,7 @@ impl ExpnData {
9751026
parent_module: Option<DefId>,
9761027
allow_internal_unsafe: bool,
9771028
local_inner_macros: bool,
978-
collapse_debuginfo: bool,
1029+
collapse_debuginfo: CollapseDebuginfo,
9791030
) -> ExpnData {
9801031
ExpnData {
9811032
kind,
@@ -1013,7 +1064,7 @@ impl ExpnData {
10131064
disambiguator: 0,
10141065
allow_internal_unsafe: false,
10151066
local_inner_macros: false,
1016-
collapse_debuginfo: false,
1067+
collapse_debuginfo: CollapseDebuginfo::Unspecified,
10171068
}
10181069
}
10191070

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ symbols! {
748748
extern_in_paths,
749749
extern_prelude,
750750
extern_types,
751+
external,
751752
external_doc,
752753
f,
753754
f16c_target_feature,
@@ -1810,6 +1811,7 @@ symbols! {
18101811
xmm_reg,
18111812
yeet_desugar_details,
18121813
yeet_expr,
1814+
yes,
18131815
yield_expr,
18141816
ymm_reg,
18151817
zmm_reg,

0 commit comments

Comments
 (0)