Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC-3086] Consider out-of-bound depths of count #111923

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ expand_meta_var_dif_seq_matchers = {$msg}
expand_meta_var_expr_unrecognized_var =
variable `{$key}` is not recognized in meta-variable expression

expand_missing_count_fragment =
related fragment that refers the `count` meta-variable expression was not found

expand_module_circular =
circular modules: {$modules}

Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,10 @@ pub struct DuplicateMatcherBinding {
#[label(expand_label2)]
pub prev: Span,
}

#[derive(Diagnostic)]
#[diag(expand_missing_count_fragment)]
pub(crate) struct MissingCountFragment {
#[primary_span]
pub span: Span,
}
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_span::Span;
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
pub(crate) enum MetaVarExpr {
/// The number of repetitions of an identifier, optionally limited to a number
/// of outer-most repetition depths. If the depth limit is `None` then the depth is unlimited.
/// of outer-most repetition depths.
Count(Ident, Option<usize>),

/// Ignore a meta-variable for repetition without expansion.
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::base::ExtCtxt;
use crate::errors::{
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
NoSyntaxVarsExprRepeat, VarStillRepeating,
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers,
MissingCountFragment, MustRepeatOnce, NoSyntaxVarsExprRepeat, VarStillRepeating,
};
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
use crate::mbe::{self, MetaVarExpr};
Expand Down Expand Up @@ -447,6 +447,9 @@ fn count_repetitions<'a>(
}
}
MatchedSeq(named_matches) => {
if named_matches.is_empty() {
return Err(cx.create_err(MissingCountFragment { span: sp.entire() }));
}
let new_declared_lhs_depth = declared_lhs_depth + 1;
match depth_opt {
None => named_matches
Expand Down Expand Up @@ -505,7 +508,7 @@ fn out_of_bounds_err<'a>(
)
} else {
format!(
"depth parameter on meta-variable expression `{ty}` \
"depth parameter of meta-variable expression `{ty}` \
must be less than {max}"
)
};
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/issue-111905.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![feature(macro_metavar_expr)]

macro_rules! foo {
($($t:ident)*) => { ${count(t, 4294967296)} };
//~^ ERROR related fragment that refers the `count` meta-variable expression was not found
}

macro_rules! bar {
( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 4294967296)} }
//~^ ERROR related fragment that refers the `count` meta-variable expression was not found
}

fn test() {
foo!();
bar!( { [] [] } );
}

fn main() {
}
14 changes: 14 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/issue-111905.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: related fragment that refers the `count` meta-variable expression was not found
--> $DIR/issue-111905.rs:4:26
|
LL | ($($t:ident)*) => { ${count(t, 4294967296)} };
| ^^^^^^^^^^^^^^^^^^^^^^

error: related fragment that refers the `count` meta-variable expression was not found
--> $DIR/issue-111905.rs:9:60
|
LL | ( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 4294967296)} }
| ^^^^^^^^^^^^^^^^^^^^^^
Comment on lines +7 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have enough information to point to the $ in the pattern which corresponds to the current level?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I wasn't able to capture the pattern span after ~1 hour of attempts


error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ macro_rules! a {
(
${count(foo, 0)},
${count(foo, 10)},
//~^ ERROR depth parameter on meta-variable expression `count` must be less than 4
//~^ ERROR depth parameter of meta-variable expression `count` must be less than 4
)
};
}
Expand All @@ -17,7 +17,7 @@ macro_rules! b {
${ignore(foo)}
${index(0)},
${index(10)},
//~^ ERROR depth parameter on meta-variable expression `index` must be less than 3
//~^ ERROR depth parameter of meta-variable expression `index` must be less than 3
)* )* )*
)
};
Expand All @@ -30,7 +30,7 @@ macro_rules! c {
${ignore(foo)}
${length(0)}
${length(10)}
//~^ ERROR depth parameter on meta-variable expression `length` must be less than 2
//~^ ERROR depth parameter of meta-variable expression `length` must be less than 2
)* )*
)
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: depth parameter on meta-variable expression `count` must be less than 4
error: depth parameter of meta-variable expression `count` must be less than 4
--> $DIR/out-of-bounds-arguments.rs:7:14
|
LL | ${count(foo, 10)},
| ^^^^^^^^^^^^^^^^

error: depth parameter on meta-variable expression `index` must be less than 3
error: depth parameter of meta-variable expression `index` must be less than 3
--> $DIR/out-of-bounds-arguments.rs:19:18
|
LL | ${index(10)},
| ^^^^^^^^^^^

error: depth parameter on meta-variable expression `length` must be less than 2
error: depth parameter of meta-variable expression `length` must be less than 2
--> $DIR/out-of-bounds-arguments.rs:32:18
|
LL | ${length(10)}
Expand Down
Loading