Implement `line!()` builtin macro
line!() is part of the list of builtin macros enumerated here
Its description is as follows:
Expands to the line number on which it was invoked.
You need to fetch the line number associated to the macro invocation:
arthur@platypus ~/G/gccrs (add-assert-macro)> cat test.rs -n
1 fn main() {
2 let l0 = line!();
3
4 println!("First invocation on line {}", l0);
5
6 let l1 = line!();
7 println!("Second on line {}", l1);
8 }
arthur@platypus ~/G/gccrs (add-assert-macro)> rustc test.rs
arthur@platypus ~/G/gccrs (add-assert-macro)> ./test
First invocation on line 2
Second on line 6
arthur@platypus ~/G/gccrs (add-assert-macro)>
The macro should be replaced by a singular node containing an integer. Each macro builtin is invoked with the macro invocation location, like so
AST::Fragment
MacroBuiltin::assert(Location invoc_locus /* <- here */, AST::MacroInvocData &invoc);
You should be able to dig around the location and find the line number. You can take a look at MacroBuiltin::file, which implements the file!() macro, for a similar operation.
Hi @CohenArthur , I've taken a shot at this in #989. Could you let me know how it looks? Thanks.
Clearing assignees since there hasn't been any activity on the PR and it's now closed. @54k1 if you still want to work on it feel free to ask to be reassigned, there's no problem at all :)
OK if no one is working on it I would like to acquire the lock on it, I had some Christmas free time to spend on some interesting new project!
There is someone working on it @CohenArthur ?
@vincenzopalazzo no one is working on it :) feel free!
Assigning you :)
@CohenArthur looks like that this macros is already implemented in https://github.com/Rust-GCC/gccrs/commit/40ad6b290af7d4e2e4d4a35a759d1b21d60423f5, but I was not able to run it with the following code
extern "C" {
fn printf(fmt: *const i8, ...);
}
fn print(s: u32) {
printf("%u\n\0" as *const str as *const i8, s);
}
macro_rules! line {
() => {{}};
}
fn main() -> i32 {
let a = line!();
print(a);
let b = line!();
print(b);
0
}
and I get this error
➜ /tmp $HOME/gccrs-install/bin/gccrs -c test.rs -o test.o -frust-incomplete-and-experimental-compiler-do-not-use
test.rs:17:11: error: expected ‘u32’ got ‘()’
7 | fn print(s: u32) {
| ~
......
17 | print(a);
| ^
test.rs:17:11: error: Type Resolution failure on parameter
test.rs:20:11: error: expected ‘u32’ got ‘()’
7 | fn print(s: u32) {
| ~
......
20 | print(b);
| ^
test.rs:20:11: error: Type Resolution failure on parameter
I'm doing something wrong?
You need the #[rustc_builtin_macro] attribute on the macro_rules! line { ... }: https://github.com/Rust-GCC/gccrs/blob/0152926ab36ba52153f3f457f6f3bb02bb274073/gcc/testsuite/rust/execute/torture/builtin_macro_line.rs#L12-L13
Thanks @bjorn3, at this point I think the issue can be closed
Oh, sorry @vincenzopalazzo I had completely forgotten that it was already implemented. I should have checked in more details. Another "easy" builtin macro you can work on if you'd like is stringify!()
@CohenArthur No problem, it is good that it is got implemented already, I will walk a couple of issue and see what sounds interesting