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

better error for bad depth parameter on macro metavar expr #99355

Merged
merged 1 commit into from
Jul 20, 2022
Merged
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
13 changes: 12 additions & 1 deletion compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,18 @@ fn out_of_bounds_err<'a>(
span: Span,
ty: &str,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
cx.struct_span_err(span, &format!("{ty} depth must be less than {max}"))
let msg = if max == 0 {
format!(
"meta-variable expression `{ty}` with depth parameter \
must be called inside of a macro repetition"
)
} else {
format!(
"depth parameter on meta-variable expression `{ty}` \
must be less than {max}"
)
};
cx.struct_span_err(span, &msg)
}

fn transcribe_metavar_expr<'a>(
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/macros/meta-variable-depth-outside-repeat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(macro_metavar_expr)]

macro_rules! metavar {
( $i:expr ) => {
${length(0)}
//~^ ERROR meta-variable expression `length` with depth parameter must be called inside of a macro repetition
};
}

const _: i32 = metavar!(0);

fn main() {}
8 changes: 8 additions & 0 deletions src/test/ui/macros/meta-variable-depth-outside-repeat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: meta-variable expression `length` with depth parameter must be called inside of a macro repetition
--> $DIR/meta-variable-depth-outside-repeat.rs:5:10
|
LL | ${length(0)}
| ^^^^^^^^^^^

error: aborting due to previous error

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 count depth must be less than 4
//~^ ERROR depth parameter on 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 index depth must be less than 3
//~^ ERROR depth parameter on meta-variable expression `index` must be less than 3
)* )* )*
)
};
Expand All @@ -30,15 +30,14 @@ macro_rules! c {
${ignore(foo)}
${length(0)}
${length(10)}
//~^ ERROR length depth must be less than 2
//~^ ERROR depth parameter on meta-variable expression `length` must be less than 2
)* )*
)
};
}


fn main() {
a!( { [ (a) ] [ (b c) ] } );
b!( { [ a b ] } );
c!( { a } );
c!({ a });
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: count depth must be less than 4
error: depth parameter on meta-variable expression `count` must be less than 4
--> $DIR/out-of-bounds-arguments.rs:7:14
|
LL | ${count(foo, 10)},
| ^^^^^^^^^^^^^^^^

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

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