tcdi / plrust

A Rust procedural language handler for PostgreSQL
PostgreSQL License
1.1k stars 32 forks source link

plrustc does not seem to catch `include!` macro properly #387

Closed anth0nyleung closed 10 months ago

anth0nyleung commented 11 months ago

Issue

plrustc has a lint to catch for filesystem macros and env macros, like include_str!, include_bytes!, env! and option_env!. It appears that the linter is not catching the include! macro usage properly.

Repro

(23-10-04 16:09:31) <0> [~/my_crate]
$ > cat /tmp/myfile.txt
['a', 'b', 'c']
    .iter()
    .cycle()
    .take(6)
    .collect::<String>()

(23-10-04 16:09:40) <0> [~/my_crate]
$ > cat src/main.rs
fn main() {
    let include = include!("/tmp/myfile.txt");
    let include_str = include_str!("/tmp/myfile.txt");
    let include_bytes = include_bytes!("/tmp/myfile.txt");
    println!("{include}");
    println!("{include_str}");
    println!("{}", String::from_utf8_lossy(include_bytes));
}

(23-10-04 16:09:45) <0> [~/my_crate]
$ >  RUSTFLAGS="-Wplrust_lints -Aplrust_external_mod -Aplrust_print_macros" RUSTC=plrustc cargo check -r
warning: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust
 --> src/main.rs:3:23
  |
3 |     let include_str = include_str!("/tmp/myfile.txt");
  |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `-W plrust-filesystem-macros` implied by `-W plrust-lints`

warning: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust
 --> src/main.rs:4:25
  |
4 |     let include_bytes = include_bytes!("/tmp/myfile.txt");
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `my_crate` (bin "my_crate") generated 2 warnings
    Finished release [optimized] target(s) in 0.00s

and

(23-10-04 15:36:40) <0> [~/github/num-bigint]
$ > RUSTFLAGS="-Wplrust_lints -Aplrust_external_mod -Aplrust_print_macros" RUSTC=plrustc cargo check
   Compiling autocfg v1.1.0
   Compiling num-traits v0.2.16
   Compiling num-integer v0.1.45
   Compiling num-bigint v0.4.4 (/local/home/antholeu/github/num-bigint)
    Finished dev [unoptimized + debuginfo] target(s) in 1.89s

(23-10-04 15:36:43) <0> [~/github/num-bigint]
$ >  grep -ir "include\!" ./*
./src/biguint/convert.rs:        include! { concat!(env!("OUT_DIR"), "/radix_bases.rs") }

https://github.com/rust-num/num-bigint/blob/num-bigint-0.4.4/src/biguint/convert.rs#L795

thomcc commented 10 months ago

This is actually unrelated to the env issues I've been having lately (which are due to issues with spans of some builtin macros). This is due to not using postgrestd, which has some extra hacks to make the include macro get detected.

Can you try with something like:

RUSTFLAGS="-Wplrust_lints -Aplrust_external_mod -Aplrust_print_macros" RUSTC=plrustc cargo check --target=x86_64-postgres-linux-gnu

That is, add --target=x86_64-postgres-linux-gnu (or another postgrestd target that works on your machine) to the invocation.

thomcc commented 10 months ago

I've tested it myself and can confirm this fixes it. Reopen if it doesn't for you.

anth0nyleung commented 10 months ago

Thanks! I tested it on my set up and adding the target flag solves the issue