public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: Philip Herron <philip.herron@embecosm.com>
To: Mark Wielaard <mark@klomp.org>
Cc: gcc-rust@gcc.gnu.org
Subject: Re: [PATCH] Reject duplicate field names in structs and unions.
Date: Sun, 22 Aug 2021 22:04:48 +0100	[thread overview]
Message-ID: <CAB2u+n1bj9ROD=LvL2mXyd+ZgRzQFw7-QpiTpVueC=u0HADu+Q@mail.gmail.com> (raw)
In-Reply-To: <20210821234748.182213-1-mark@klomp.org>

[-- Attachment #1: Type: text/plain, Size: 6251 bytes --]

Nice work once again, just to let you know about the little bit of
duplication I think the name resolver needs a little bit of thought to
handle forward declared items within a block like this:
https://github.com/Rust-GCC/gccrs/issues/531. It might be as simple as
calling ResolveToplevelItems when doing block expr resolution. Depending on
how we fix this it might feed back a way to avoid this little bit of
duplication.

Thanks

--Phil

On Sun, 22 Aug 2021 at 00:48, Mark Wielaard <mark@klomp.org> wrote:

> Odd things happen if structs or unions have duplicate field names.
> Emit an error when lowering an struct/union item or declaration
> statement and a duplicate field name is detected.  A new testcase
> 'dup_fields.rs' checks an error is actually produced.
> ---
>
> https://code.wildebeest.org/git/user/mjw/gccrs/commit/?h=dup_fields
>
>  gcc/rust/hir/rust-ast-lower-item.h       | 27 ++++++++++++++++++++++++
>  gcc/rust/hir/rust-ast-lower-stmt.h       | 27 ++++++++++++++++++++++++
>  gcc/testsuite/rust/compile/dup_fields.rs | 23 ++++++++++++++++++++
>  3 files changed, 77 insertions(+)
>  create mode 100644 gcc/testsuite/rust/compile/dup_fields.rs
>
> diff --git a/gcc/rust/hir/rust-ast-lower-item.h
> b/gcc/rust/hir/rust-ast-lower-item.h
> index bcf83ee63b6..b324d0b5b8b 100644
> --- a/gcc/rust/hir/rust-ast-lower-item.h
> +++ b/gcc/rust/hir/rust-ast-lower-item.h
> @@ -176,6 +176,25 @@ public:
>                                struct_decl.get_locus ());
>    }
>
> +  /* Checks whether the name of a field already exists.  Returns true
> +     and produces an error if so.  */
> +  static bool struct_field_name_exists (std::vector<HIR::StructField>
> &fields,
> +                                       HIR::StructField &new_field)
> +  {
> +    for (auto &field : fields)
> +      {
> +       if (field.get_field_name ().compare (new_field.get_field_name ())
> == 0)
> +         {
> +           RichLocation r (new_field.get_locus ());
> +           r.add_range (field.get_locus ());
> +           rust_error_at (r, "duplicate field name %qs",
> +                          field.get_field_name ().c_str ());
> +           return true;
> +         }
> +      }
> +    return false;
> +  }
> +
>    void visit (AST::StructStruct &struct_decl) override
>    {
>      std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
> @@ -206,6 +225,10 @@ public:
>                                          std::unique_ptr<HIR::Type>
> (type), vis,
>                                          field.get_locus (),
>                                          field.get_outer_attrs ());
> +
> +      if (struct_field_name_exists (fields, translated_field))
> +       return false;
> +
>        fields.push_back (std::move (translated_field));
>        return true;
>      });
> @@ -258,6 +281,10 @@ public:
>                                            std::unique_ptr<HIR::Type>
> (type),
>                                            vis, variant.get_locus (),
>                                            variant.get_outer_attrs ());
> +
> +      if (struct_field_name_exists (variants, translated_variant))
> +       return false;
> +
>        variants.push_back (std::move (translated_variant));
>        return true;
>      });
> diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h
> b/gcc/rust/hir/rust-ast-lower-stmt.h
> index c4c00ac0bee..1e72c8a2023 100644
> --- a/gcc/rust/hir/rust-ast-lower-stmt.h
> +++ b/gcc/rust/hir/rust-ast-lower-stmt.h
> @@ -161,6 +161,25 @@ public:
>                                struct_decl.get_locus ());
>    }
>
> +  /* Checks whether the name of a field already exists.  Returns true
> +     and produces an error if so.  */
> +  static bool struct_field_name_exists (std::vector<HIR::StructField>
> &fields,
> +                                       HIR::StructField &new_field)
> +  {
> +    for (auto &field : fields)
> +      {
> +       if (field.get_field_name ().compare (new_field.get_field_name ())
> == 0)
> +         {
> +           RichLocation r (new_field.get_locus ());
> +           r.add_range (field.get_locus ());
> +           rust_error_at (r, "duplicate field name %qs",
> +                          field.get_field_name ().c_str ());
> +           return true;
> +         }
> +      }
> +    return false;
> +  }
> +
>    void visit (AST::StructStruct &struct_decl) override
>    {
>      std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
> @@ -191,6 +210,10 @@ public:
>                                          std::unique_ptr<HIR::Type>
> (type), vis,
>                                          field.get_locus (),
>                                          field.get_outer_attrs ());
> +
> +      if (struct_field_name_exists (fields, translated_field))
> +       return false;
> +
>        fields.push_back (std::move (translated_field));
>        return true;
>      });
> @@ -242,6 +265,10 @@ public:
>                                            std::unique_ptr<HIR::Type>
> (type),
>                                            vis, variant.get_locus (),
>                                            variant.get_outer_attrs ());
> +
> +      if (struct_field_name_exists (variants, translated_variant))
> +       return false;
> +
>        variants.push_back (std::move (translated_variant));
>        return true;
>      });
> diff --git a/gcc/testsuite/rust/compile/dup_fields.rs
> b/gcc/testsuite/rust/compile/dup_fields.rs
> new file mode 100644
> index 00000000000..ab39955eca0
> --- /dev/null
> +++ b/gcc/testsuite/rust/compile/dup_fields.rs
> @@ -0,0 +1,23 @@
> +struct S { a: i32, b: i32, c: u8, a: i128 }
> +// { dg-error "duplicate field" "" { target *-*-* } .-1 }
> +
> +union U
> +  {
> +    a: i32,
> +    b: i32,
> +    c: u8,
> +    b: char // { dg-error "duplicate field" "" { target *-*-* } }
> +  }
> +
> +fn main ()
> +{
> +  struct SS { alpha: i32, beta: i32, gamma: u8, gamma: i128 }
> +  // { dg-error "duplicate field" "" { target *-*-* } .-1 }
> +
> +  union UU
> +    {
> +      alpha: i32, beta: i32,
> +      gamma: u8, beta: char
> +      // { dg-error "duplicate field" "" { target *-*-* } .-1 }
> +    }
> +}
> --
> 2.32.0
>
> --
> Gcc-rust mailing list
> Gcc-rust@gcc.gnu.org
> https://gcc.gnu.org/mailman/listinfo/gcc-rust
>

[-- Attachment #2: Type: text/html, Size: 8560 bytes --]

      reply	other threads:[~2021-08-22 21:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-21 23:47 Mark Wielaard
2021-08-22 21:04 ` Philip Herron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAB2u+n1bj9ROD=LvL2mXyd+ZgRzQFw7-QpiTpVueC=u0HADu+Q@mail.gmail.com' \
    --to=philip.herron@embecosm.com \
    --cc=gcc-rust@gcc.gnu.org \
    --cc=mark@klomp.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).