From 530fba8c0823214cabdf92ab0920450d6f69f2ce Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 2 Nov 2023 12:30:13 -0700 Subject: [PATCH 1/5] Try tweaking the env def-path and unconditionally exposing the env diagnostic item --- plrust/build | 2 +- plrustc/plrustc/src/lints/builtin_macros.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/plrust/build b/plrust/build index 4d3fa2d6..bc5e0db6 100755 --- a/plrust/build +++ b/plrust/build @@ -75,7 +75,7 @@ fi git pull git submodule update --init --recursive else - git clone https://github.com/tcdi/postgrestd.git --branch "rust-1.73.0" --recurse-submodules + git clone https://github.com/tcdi/postgrestd.git --branch "thomcc/uncond-env" --recurse-submodules cd ./postgrestd fi rm -f rust-toolchain.toml diff --git a/plrustc/plrustc/src/lints/builtin_macros.rs b/plrustc/plrustc/src/lints/builtin_macros.rs index 123c4fb1..8daaaec4 100644 --- a/plrustc/plrustc/src/lints/builtin_macros.rs +++ b/plrustc/plrustc/src/lints/builtin_macros.rs @@ -61,6 +61,10 @@ impl PlrustBuiltinMacros { let env_def_paths: &[&[Symbol]] = &[ &[sym!(core), sym!(macros), sym!(builtin), sym!(env)], &[sym!(core), sym!(macros), sym!(builtin), sym!(option_env)], + &[sym!(core), sym!(macros), sym!(env)], + &[sym!(core), sym!(macros), sym!(option_env)], + &[sym!(core), sym!(env)], + &[sym!(core), sym!(option_env)], ]; if let Some((s, ..)) = utils::check_span_against_macro_def_paths(cx, span, &env_def_paths) { self.lint_env(cx, s); From 1d8882aac9c7cdd83e249ca319b0a61db6821915 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 2 Nov 2023 12:51:12 -0700 Subject: [PATCH 2/5] Relax the env/option_env panic message (DNM) --- plrust-tests/src/trusted.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plrust-tests/src/trusted.rs b/plrust-tests/src/trusted.rs index 33b373a6..506de7ab 100644 --- a/plrust-tests/src/trusted.rs +++ b/plrust-tests/src/trusted.rs @@ -190,7 +190,7 @@ mod tests { #[pg_test] #[search_path(@extschema@)] - #[should_panic = "error: the `env` and `option_env` macros are forbidden"] + #[should_panic] // = "error: the `env` and `option_env` macros are forbidden"] #[cfg(feature = "trusted")] fn plrust_block_env() -> spi::Result<()> { let definition = r#" @@ -204,7 +204,7 @@ mod tests { #[pg_test] #[search_path(@extschema@)] - #[should_panic = "error: the `env` and `option_env` macros are forbidden"] + #[should_panic] // = "error: the `env` and `option_env` macros are forbidden"] #[cfg(feature = "trusted")] fn plrust_block_option_env() -> spi::Result<()> { let definition = r#" From 44e7f04de6b4686ce4a917948039295c4eeaabe8 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 2 Nov 2023 14:22:02 -0700 Subject: [PATCH 3/5] Change how spans are emitted in the builtin macro lints --- plrustc/plrustc/src/lints/builtin_macros.rs | 20 +++- plrustc/plrustc/uitests/fs_macros.stderr | 124 +++++++++++++++++++- 2 files changed, 137 insertions(+), 7 deletions(-) diff --git a/plrustc/plrustc/src/lints/builtin_macros.rs b/plrustc/plrustc/src/lints/builtin_macros.rs index 8daaaec4..dbc271a7 100644 --- a/plrustc/plrustc/src/lints/builtin_macros.rs +++ b/plrustc/plrustc/src/lints/builtin_macros.rs @@ -39,7 +39,9 @@ impl PlrustBuiltinMacros { if let Some((s, ..)) = utils::check_span_against_macro_diags(cx, span, &fs_diagnostic_items) { self.lint_fs(cx, s); - return; + if span != s { + self.lint_fs(cx, span); + } } let fs_def_paths: &[&[Symbol]] = &[ &[sym!(core), sym!(macros), sym!(builtin), sym!(include)], @@ -48,15 +50,19 @@ impl PlrustBuiltinMacros { ]; if let Some((s, ..)) = utils::check_span_against_macro_def_paths(cx, span, &fs_def_paths) { self.lint_fs(cx, s); - return; + if span != s { + self.lint_fs(cx, span); + } } let env_diagnostic_items = [sym!(env_macro), sym!(option_env_macro)]; if let Some((s, ..)) = utils::check_span_against_macro_diags(cx, span, &env_diagnostic_items) { - self.lint_env(cx, s); - return; + self.lint_env(cx, span); + if span != s { + self.lint_env(cx, span); + } } let env_def_paths: &[&[Symbol]] = &[ &[sym!(core), sym!(macros), sym!(builtin), sym!(env)], @@ -67,8 +73,10 @@ impl PlrustBuiltinMacros { &[sym!(core), sym!(option_env)], ]; if let Some((s, ..)) = utils::check_span_against_macro_def_paths(cx, span, &env_def_paths) { - self.lint_env(cx, s); - return; + self.lint_env(cx, span); + if span != s { + self.lint_env(cx, span); + } } } } diff --git a/plrustc/plrustc/uitests/fs_macros.stderr b/plrustc/plrustc/uitests/fs_macros.stderr index f2b2c741..b0368c59 100644 --- a/plrustc/plrustc/uitests/fs_macros.stderr +++ b/plrustc/plrustc/uitests/fs_macros.stderr @@ -6,23 +6,55 @@ LL | const _A: &str = include_str!("fs_macros_included_file.txt"); | = note: `-F plrust-filesystem-macros` implied by `-F plrust-lints` +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:3:18 + | +LL | const _A: &str = include_str!("fs_macros_included_file.txt"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:5:19 + | +LL | const _B: &[u8] = include_bytes!("fs_macros_included_file.txt"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:5:19 | LL | const _B: &[u8] = include_bytes!("fs_macros_included_file.txt"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:7:18 + | +LL | const _C: &str = core::include_str!("fs_macros_included_file.txt"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:7:18 | LL | const _C: &str = core::include_str!("fs_macros_included_file.txt"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `core::include_str` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:8:19 + | +LL | const _D: &[u8] = core::include_bytes!("fs_macros_included_file.txt"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:8:19 | LL | const _D: &[u8] = core::include_bytes!("fs_macros_included_file.txt"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `core::include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:16:18 @@ -30,23 +62,64 @@ error: the `include_str`, `include_bytes`, and `include` macros are forbidden in LL | const _E: &str = indirect!(include_str); | ^^^^^^^^^^^^^^^^^^^^^^ +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:12:9 + | +LL | $callme!("fs_macros_included_file.txt") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | const _E: &str = indirect!(include_str); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `include_str` which comes from the expansion of the macro `indirect` (in Nightly builds, run with -Z macro-backtrace for more info) + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:17:19 | LL | const _F: &[u8] = indirect!(include_bytes); | ^^^^^^^^^^^^^^^^^^^^^^^^ +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:12:9 + | +LL | $callme!("fs_macros_included_file.txt") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | const _F: &[u8] = indirect!(include_bytes); + | ------------------------ in this macro invocation + | + = note: this error originates in the macro `include_bytes` which comes from the expansion of the macro `indirect` (in Nightly builds, run with -Z macro-backtrace for more info) + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:31:18 | LL | const _G: &str = in_macro!(); | ^^^^^^^^^^^ +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:21:9 + | +LL | include_str!("fs_macros_included_file.txt") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | const _G: &str = in_macro!(); + | ----------- in this macro invocation + | + = note: this error originates in the macro `include_str` which comes from the expansion of the macro `in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:32:28 + | +LL | const _H: &str = in_macro!(include_str!("fs_macros_included_file.txt")); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:32:28 | LL | const _H: &str = in_macro!(include_str!("fs_macros_included_file.txt")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info) error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:33:29 @@ -54,17 +127,44 @@ error: the `include_str`, `include_bytes`, and `include` macros are forbidden in LL | const _I: &[u8] = in_macro!(include_bytes!("fs_macros_included_file.txt")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:33:29 + | +LL | const _I: &[u8] = in_macro!(include_bytes!("fs_macros_included_file.txt")); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:34:19 | LL | const _J: &[u8] = in_macro!(include_bytes, "fs_macros_included_file.txt"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:27:9 + | +LL | $mac!($arg) + | ^^^^^^^^^^^ +... +LL | const _J: &[u8] = in_macro!(include_bytes, "fs_macros_included_file.txt"); + | ------------------------------------------------------- in this macro invocation + | + = note: this error originates in the macro `include_bytes` which comes from the expansion of the macro `in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:37:18 + | +LL | const _L: &str = sneaky!("fs_macros_included_file.txt"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:37:18 | LL | const _L: &str = sneaky!("fs_macros_included_file.txt"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `sneaky` (in Nightly builds, run with -Z macro-backtrace for more info) error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:38:18 @@ -72,11 +172,33 @@ error: the `include_str`, `include_bytes`, and `include` macros are forbidden in LL | const _M: &str = in_macro!(sneaky, "fs_macros_included_file.txt"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:27:9 + | +LL | $mac!($arg) + | ^^^^^^^^^^^ +... +LL | const _M: &str = in_macro!(sneaky, "fs_macros_included_file.txt"); + | ------------------------------------------------ in this macro invocation + | + = note: this error originates in the macro `sneaky` which comes from the expansion of the macro `in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) + error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust --> $DIR/fs_macros.rs:41:21 | LL | format!("{:?}", in_macro!()) | ^^^^^^^^^^^ -error: aborting due to 13 previous errors +error: the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust + --> $DIR/fs_macros.rs:21:9 + | +LL | include_str!("fs_macros_included_file.txt") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | format!("{:?}", in_macro!()) + | ----------- in this macro invocation + | + = note: this error originates in the macro `include_str` which comes from the expansion of the macro `in_macro` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 26 previous errors From c07288abacfc3112ffac18a853f3fc791108f6dc Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 2 Nov 2023 14:27:51 -0700 Subject: [PATCH 4/5] Ensure `report_in_external_macro` is enabled --- plrustc/plrustc/src/lints/utils.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/plrustc/plrustc/src/lints/utils.rs b/plrustc/plrustc/src/lints/utils.rs index cbeb77fa..1f55dcf8 100644 --- a/plrustc/plrustc/src/lints/utils.rs +++ b/plrustc/plrustc/src/lints/utils.rs @@ -10,13 +10,25 @@ macro_rules! declare_plrust_lint { $v:vis $NAME:ident, $desc:expr $(,)? ) => { - rustc_lint_defs::declare_lint! ( - $(#[$attr])* - $v $NAME, - Allow, - $desc, - report_in_external_macro - ); + $(#[$attr])* + $v static $NAME: &rustc_lint_defs::Lint = &rustc_lint_defs::Lint { + name: stringify!($NAME), + default_level: rustc_lint_defs::Allow, + desc: $desc, + edition_lint_opts: None, + report_in_external_macro: true, + is_plugin: false, + crate_level_only: false, + ..rustc_lint_defs::Lint::default_fields_for_macro() + }; + // ); + // rustc_lint_defs::declare_lint! ( + // $(#[$attr])* + // $v $NAME, + // Allow, + // $desc, + // report_in_external_macro + // ); }; } @@ -133,7 +145,7 @@ pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) - pub fn has_fn_trait(cx: &LateContext<'_>, poly_trait: &hir::PolyTraitRef<'_>) -> bool { let Some(impl_did) = poly_trait.trait_ref.path.res.opt_def_id() else { - return false + return false; }; let lang_items = cx.tcx.lang_items(); let fntraits = [ From 4446e1efa788cc0c98324e10037918cd823f2192 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Thu, 2 Nov 2023 14:43:19 -0700 Subject: [PATCH 5/5] actually emit the lint with the right span the second time --- plrustc/plrustc/src/lints/builtin_macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plrustc/plrustc/src/lints/builtin_macros.rs b/plrustc/plrustc/src/lints/builtin_macros.rs index dbc271a7..6220b909 100644 --- a/plrustc/plrustc/src/lints/builtin_macros.rs +++ b/plrustc/plrustc/src/lints/builtin_macros.rs @@ -61,7 +61,7 @@ impl PlrustBuiltinMacros { { self.lint_env(cx, span); if span != s { - self.lint_env(cx, span); + self.lint_env(cx, s); } } let env_def_paths: &[&[Symbol]] = &[ @@ -75,7 +75,7 @@ impl PlrustBuiltinMacros { if let Some((s, ..)) = utils::check_span_against_macro_def_paths(cx, span, &env_def_paths) { self.lint_env(cx, span); if span != s { - self.lint_env(cx, span); + self.lint_env(cx, s); } } }