Improved collapse_debuginfo attribute, added command-line flag (no|ex… · rust-lang/rust@8507f51 (original) (raw)

1

1

`#![deny(rustc::untranslatable_diagnostic)]

`

2

2

``

``

3

`+

use crate::base::ast::NestedMetaItem;

`

3

4

`use crate::errors;

`

4

5

`use crate::expand::{self, AstFragment, Invocation};

`

5

6

`use crate::module::DirOwnership;

`

`@@ -19,6 +20,7 @@ use rustc_feature::Features;

`

19

20

`use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;

`

20

21

`use rustc_lint_defs::{BufferedEarlyLint, BuiltinLintDiagnostics, RegisteredTools};

`

21

22

`use rustc_parse::{parser, MACRO_ARGUMENTS};

`

``

23

`+

use rustc_session::config::CollapseMacroDebuginfo;

`

22

24

`use rustc_session::errors::report_lit_error;

`

23

25

`use rustc_session::{parse::ParseSess, Limit, Session};

`

24

26

`use rustc_span::def_id::{CrateNum, DefId, LocalDefId};

`

`@@ -761,6 +763,55 @@ impl SyntaxExtension {

`

761

763

`}

`

762

764

`}

`

763

765

``

``

766

`+

fn collapse_debuginfo_by_name(sess: &Session, attr: &Attribute) -> CollapseMacroDebuginfo {

`

``

767

`+

use crate::errors::CollapseMacroDebuginfoIllegal;

`

``

768

`+

// #[collapse_debuginfo] without enum value (#[collapse_debuginfo(no/external/yes)])

`

``

769

`` +

// considered as yes

``

``

770

`+

attr.meta_item_list().map_or(CollapseMacroDebuginfo::Yes, |l| {

`

``

771

`+

let [NestedMetaItem::MetaItem(item)] = &l[..] else {

`

``

772

`+

sess.dcx().emit_err(CollapseMacroDebuginfoIllegal { span: attr.span });

`

``

773

`+

return CollapseMacroDebuginfo::Unspecified;

`

``

774

`+

};

`

``

775

`+

if !item.is_word() {

`

``

776

`+

sess.dcx().emit_err(CollapseMacroDebuginfoIllegal { span: item.span });

`

``

777

`+

CollapseMacroDebuginfo::Unspecified

`

``

778

`+

} else {

`

``

779

`+

match item.name_or_empty() {

`

``

780

`+

sym::no => CollapseMacroDebuginfo::No,

`

``

781

`+

sym::external => CollapseMacroDebuginfo::External,

`

``

782

`+

sym::yes => CollapseMacroDebuginfo::Yes,

`

``

783

`+

_ => {

`

``

784

`+

sess.dcx().emit_err(CollapseMacroDebuginfoIllegal { span: item.span });

`

``

785

`+

CollapseMacroDebuginfo::Unspecified

`

``

786

`+

}

`

``

787

`+

}

`

``

788

`+

}

`

``

789

`+

})

`

``

790

`+

}

`

``

791

+

``

792

`+

/// if-ext - if macro from different crate (related to callsite code)

`

``

793

`+

/// | cmd \ attr | no | (unspecified) | external | yes |

`

``

794

`+

/// | no | no | no | no | no |

`

``

795

`+

/// | (unspecified) | no | no | if-ext | yes |

`

``

796

`+

/// | external | no | if-ext | if-ext | yes |

`

``

797

`+

/// | yes | yes | yes | yes | yes |

`

``

798

`+

fn get_collapse_debuginfo(sess: &Session, attrs: &[ast::Attribute], is_local: bool) -> bool {

`

``

799

`+

let collapse_debuginfo_attr = attr::find_by_name(attrs, sym::collapse_debuginfo)

`

``

800

`+

.map(|v| Self::collapse_debuginfo_by_name(sess, v))

`

``

801

`+

.unwrap_or(CollapseMacroDebuginfo::Unspecified);

`

``

802

`+

let flag = sess.opts.unstable_opts.collapse_macro_debuginfo;

`

``

803

`+

let attr = collapse_debuginfo_attr;

`

``

804

`+

let ext = !is_local;

`

``

805

`+

#[rustfmt::skip]

`

``

806

`+

let collapse_table = [

`

``

807

`+

[false, false, false, false],

`

``

808

`+

[false, false, ext, true],

`

``

809

`+

[false, ext, ext, true],

`

``

810

`+

[true, true, true, true],

`

``

811

`+

];

`

``

812

`+

collapse_table[flag as usize][attr as usize]

`

``

813

`+

}

`

``

814

+

764

815

`/// Constructs a syntax extension with the given properties

`

765

816

`/// and other properties converted from attributes.

`

766

817

`pub fn new(

`

`@@ -772,6 +823,7 @@ impl SyntaxExtension {

`

772

823

`edition: Edition,

`

773

824

`name: Symbol,

`

774

825

`attrs: &[ast::Attribute],

`

``

826

`+

is_local: bool,

`

775

827

`) -> SyntaxExtension {

`

776

828

`let allow_internal_unstable =

`

777

829

` attr::allow_internal_unstable(sess, attrs).collect::<Vec>();

`

`@@ -780,8 +832,8 @@ impl SyntaxExtension {

`

780

832

`let local_inner_macros = attr::find_by_name(attrs, sym::macro_export)

`

781

833

`.and_then(|macro_export| macro_export.meta_item_list())

`

782

834

`.is_some_and(|l| attr::list_contains_name(&l, sym::local_inner_macros));

`

783

``

`-

let collapse_debuginfo = attr::contains_name(attrs, sym::collapse_debuginfo);

`

784

``

`-

tracing::debug!(?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);

`

``

835

`+

let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, is_local);

`

``

836

`+

tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);

`

785

837

``

786

838

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

`

787

839

`.map(|attr| {

`