Skip to content

Commit

Permalink
impl new way to generate the compiler suggestion
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jun 8, 2023
1 parent 2361904 commit 7ddb63a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 27 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ expand_duplicate_matcher_binding = duplicate matcher binding
.label = duplicate binding
.label2 = previous binding
expand_expansion_growth_limit_reached =
expansion grow limit reached while expanding `{$descr}`
.help = consider increasing the expansion grow limit by adding a `#![expansion_growth_limit = "{$suggested_limit}"]` attribute to your crate (`{$crate_name}`)
expand_expected_comma_in_list =
expected token: `,`
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ pub(crate) struct RecursionLimitReached<'a> {
pub crate_name: &'a str,
}

#[derive(Diagnostic)]
#[diag(expand_expansion_growth_limit_reached)]
#[help]
pub(crate) struct ExpansionGrowthLimitReached<'a> {
#[primary_span]
pub span: Span,
pub descr: String,
pub suggested_limit: Limit,
pub crate_name: &'a str,
}

#[derive(Diagnostic)]
#[diag(expand_malformed_feature_attribute, code = "E0556")]
pub(crate) struct MalformedFeatureAttribute {
Expand Down
31 changes: 10 additions & 21 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::base::*;
use crate::config::StripUnconfigured;
use crate::errors::{
IncompleteParse, RecursionLimitReached, RemoveExprNotSupported, RemoveNodeNotSupported,
UnsupportedKeyValue, WrongFragmentKind,
ExpansionGrowthLimitReached, IncompleteParse, RecursionLimitReached, RemoveExprNotSupported,
RemoveNodeNotSupported, UnsupportedKeyValue, WrongFragmentKind,
};
use crate::hygiene::SyntaxContext;
use crate::mbe::diagnostics::annotate_err_with_kind;
Expand Down Expand Up @@ -628,20 +628,12 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
limit => limit * 2,
};

self.cx
.struct_span_err(
expn_data.call_site,
&format!(
"expansion grow limit reached while expanding `{}`",
expn_data.kind.descr()
),
)
.help(&format!(
"consider increasing the expansion grow limit by adding a \
`#![expansion_growth_limit = \"{}\"]` attribute to your crate (`{}`)",
suggested_limit, self.cx.ecfg.crate_name,
))
.emit();
self.cx.emit_err(ExpansionGrowthLimitReached {
span: expn_data.call_site,
descr: expn_data.kind.descr(),
suggested_limit,
crate_name: &self.cx.ecfg.crate_name,
});
self.cx.trace_macros_diag();
}

Expand All @@ -663,7 +655,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {

// Reduce the recursion limit by half each time it triggers.
self.cx.reduced_recursion_limit = Some(expansion_limit / 2);

return Err(());
}
Ok(())
Expand Down Expand Up @@ -697,11 +688,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.parse_ast_fragment(tok_result, fragment_kind, &mac.path, span)
}
SyntaxExtensionKind::LegacyBang(expander) => {
if self.reduce_expansion_growth_limit(mac.args.inner_tokens().len()).is_err() {
if self.reduce_expansion_growth_limit(mac.args.tokens.len()).is_err() {
return ExpandResult::Ready(fragment_kind.dummy(span));
}
let prev = self.cx.current_expansion.prior_type_ascription;
self.cx.current_expansion.prior_type_ascription = mac.prior_type_ascription;
let tok_result = expander.expand(self.cx, span, mac.args.tokens.clone());
let result = if let Some(result) = fragment_kind.make_from(tok_result) {
result
Expand Down Expand Up @@ -2027,7 +2016,7 @@ impl<'feat> ExpansionConfig<'feat> {
crate_name,
features: None,
recursion_limit: Limit::new(1024),
expansion_growth_limit: Limit::new(6000),
expansion_growth_limit: Limit::new(1000000),
trace_mac: false,
should_test: false,
span_debug: false,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn get_recursion_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
}

pub fn get_expansion_growth_limit(krate_attrs: &[Attribute], sess: &Session) -> Limit {
get_limit(krate_attrs, sess, sym::expansion_growth_limit, 6000)
get_limit(krate_attrs, sess, sym::expansion_growth_limit, 1000000)
}

fn get_limit(krate_attrs: &[Attribute], sess: &Session, name: Symbol, default: usize) -> Limit {
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ pub struct Limits {
pub move_size_limit: Limit,
/// The maximum length of types during monomorphization.
pub type_length_limit: Limit,
/// The maximum blocks a const expression can evaluate.
pub const_eval_limit: Limit,
/// The maximum tokens limit for potentially infinitely resolving
/// a macros that add infinite tokens inside the buffer.
pub expansion_growth_limit: Limit,
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/limits/issue-95698.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// check-fail

/// issue #95698
// issue #95698
macro_rules! from_cow_impls {
($( $from: ty ),+ $(,)? ) => {
// recursion call
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/limits/issue-95698.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | | Vec<u8>, /*callback,*/
LL | | );
| |_- in this macro invocation
|
= help: consider increasing the expansion grow limit by adding a `#![expansion_growth_limit = "12000"]` attribute to your crate (`issue_95698`)
= help: consider increasing the expansion grow limit by adding a `#![expansion_growth_limit = "2000000"]` attribute to your crate (`issue_95698`)
= note: this error originates in the macro `from_cow_impls` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
Expand Down
1 change: 0 additions & 1 deletion tests/ui/mir/issue-29227.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![expansion_growth_limit = "16000"]
// run-pass
// ignore-tidy-linelength

Expand Down

0 comments on commit 7ddb63a

Please sign in to comment.