Skip to content

Commit

Permalink
Switch to anon-const for wrapping expanded impls
Browse files Browse the repository at this point in the history
Fixes a non_local_definitions warning in the generated code.

    warning: non-local `impl` definition, they should be avoided as they go against expectation
      --> tests/test.rs:18:5
       |
    18 |     #[typetag::serde]
       |     ^^^^^^^^^^^^^^^^^
       |
       = help: move this `impl` block outside the of the current constant `_Trait_registry`
       = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
       = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
       = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
       = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
       = note: `#[warn(non_local_definitions)]` on by default
       = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
      --> tests/test.rs:18:5
       |
    18 |     #[typetag::serde]
       |     ^^^^^^^^^^^^^^^^^
       |
       = help: move this `impl` block outside the of the current constant `_Trait_registry`
       = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
       = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
       = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
       = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
       = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
      --> tests/test.rs:71:5
       |
    71 |     #[typetag::serde(tag = "type")]
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |
       = help: move this `impl` block outside the of the current constant `_Trait_registry`
       = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
       = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
       = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
       = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
       = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:124:5
        |
    124 |     #[typetag::serde(tag = "type", content = "content")]
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Trait_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:192:5
        |
    192 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Trait_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:278:5
        |
    278 |     #[typetag::serde(tag = "type", default_variant = "A")]
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Trait_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:322:5
        |
    322 |     #[typetag::serde(tag = "type", content = "content", default_variant = "A")]
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Trait_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:366:5
        |
    366 |     #[typetag::serde(tag = "type", content = "content", deny_unknown_fields)]
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Trait_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:393:5
        |
    393 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Neither_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:396:5
        |
    396 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Sendable_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:399:5
        |
    399 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Syncable_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:402:5
        |
    402 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Both_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:459:5
        |
    459 |     #[typetag::serialize]
        |     ^^^^^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Generic_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serialize` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serialize` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:466:5
        |
    466 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Trait_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:483:5
        |
    483 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Base_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)

    warning: non-local `impl` definition, they should be avoided as they go against expectation
       --> tests/test.rs:492:5
        |
    492 |     #[typetag::serde]
        |     ^^^^^^^^^^^^^^^^^
        |
        = help: move this `impl` block outside the of the current constant `_Derived_registry`
        = note: an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
        = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
        = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <rust-lang/rust#120363>
        = note: the attribute macro `typetag::serde` may come from an old version of the `typetag_impl` crate, try updating your dependency with `cargo update -p typetag_impl`
        = note: this warning originates in the attribute macro `typetag::serde` (in Nightly builds, run with -Z macro-backtrace for more info)
  • Loading branch information
dtolnay committed Feb 26, 2024
1 parent bfe5e72 commit d51f4ad
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions impl/src/tagged_trait.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Mode, TraitArgs};
use proc_macro2::{Span, TokenStream};
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Error, Ident, ItemTrait, LitStr, TraitBoundModifier, TypeParamBound};
use syn::{parse_quote, Error, ItemTrait, LitStr, TraitBoundModifier, TypeParamBound};

pub(crate) fn expand(args: TraitArgs, mut input: ItemTrait, mode: Mode) -> TokenStream {
if mode.de && !input.generics.params.is_empty() {
Expand Down Expand Up @@ -279,14 +279,11 @@ fn has_supertrait(input: &ItemTrait, find: &str) -> bool {
}

fn wrap_in_dummy_const(input: ItemTrait, expanded: TokenStream) -> TokenStream {
let dummy_const_name = format!("_{}_registry", input.ident);
let dummy_const = Ident::new(&dummy_const_name, Span::call_site());

quote! {
#input

#[allow(non_upper_case_globals)]
const #dummy_const: () = {
const _: () = {
#expanded
};
}
Expand Down

0 comments on commit d51f4ad

Please sign in to comment.