Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 3351: Shared discriminator having the same value< #3458

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gcc/rust/backend/rust-compile-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ CompileItem::visit (HIR::Function &function)
ctx->pop_const_context ();
}

void
CompileItem::visit (HIR::Enum &e)
{
TyTy::BaseType *resolved_type = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (e.get_mappings ().get_hirid (),
&resolved_type);
rust_assert (ok);

tree type = TyTyResolveCompile::compile (ctx, resolved_type);
reference = type;
}
void
CompileItem::visit (HIR::ImplBlock &impl_block)
{
Expand Down
3 changes: 1 addition & 2 deletions gcc/rust/backend/rust-compile-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CompileItem : private HIRCompileBase, protected HIR::HIRStmtVisitor
void visit (HIR::ImplBlock &impl_block) override;
void visit (HIR::ExternBlock &extern_block) override;
void visit (HIR::Module &module) override;

void visit (HIR::Enum &) override;
// Empty visit for unused Stmt HIR nodes.
void visit (HIR::TupleStruct &) override {}
void visit (HIR::EnumItem &) override {}
Expand All @@ -62,7 +62,6 @@ class CompileItem : private HIRCompileBase, protected HIR::HIRStmtVisitor
void visit (HIR::UseDeclaration &) override {}
void visit (HIR::TypeAlias &) override {}
void visit (HIR::StructStruct &) override {}
void visit (HIR::Enum &) override {}
void visit (HIR::Union &) override {}
void visit (HIR::Trait &) override {}
void visit (HIR::EmptyStmt &) override {}
Expand Down
72 changes: 67 additions & 5 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include "rust-compile-expr.h"
#include "rust-constexpr.h"
#include "rust-gcc.h"
#include "rust-diagnostics.h"

#include "tree.h"
#include "stor-layout.h"

namespace Rust {
namespace Compile {

Expand Down Expand Up @@ -239,14 +239,70 @@ TyTyResolveCompile::visit (const TyTy::FnPtr &type)
type.get_ident ().locus);
}

bool
check_variant_record_collision (Context *ctx, const TyTy::ADTType &type,
std::vector<tree> &variant_records)
{
// bdbt: we're checking if shared discriminants crash with each other or
// not. lets make a map from uhwi to hir id. A clash of uhwi in a variant
// record to which said record can be converted uhwi is indicative of
// issue 3351 of gccrs

std::map<HOST_WIDE_INT, std::vector<size_t>> shwi_to_index;
for (size_t i = 0; i < variant_records.size (); i++)
{
TyTy::VariantDef *variant = type.get_variants ().at (i);
if (variant->has_discriminant ())
{
tree discriminant_expr
= CompileExpr::Compile (variant->get_discriminant (), ctx);
tree folded_expr = fold_expr (discriminant_expr);
if (folded_expr == error_mark_node)
{
// if we have discriminant but we fail to fold it, return false
return false;
}
HOST_WIDE_INT discriminant_integer = tree_to_shwi (folded_expr);
shwi_to_index[discriminant_integer].push_back (i);
}
}

bool has_failed = false;
for (const auto &map_item : shwi_to_index)
{
auto discriminant_integer = map_item.first;
const auto &index_vector = map_item.second;
// collision doesn't happen, move to next item
if (index_vector.size () <= 1)
continue;

has_failed = true;
rich_location r (line_table, type.get_locus ());
std::string assigned_here_msg
= expand_message (HOST_WIDE_INT_PRINT_DEC " assigned here",
discriminant_integer);
std::string assigned_more_once_msg
= expand_message ("discriminant value " HOST_WIDE_INT_PRINT_DEC
" assigned more than once",
discriminant_integer);
for (auto index : index_vector)
{
TyTy::VariantDef *variant = type.get_variants ().at (index);
r.add_fixit_replace (variant->get_discriminant ().get_locus (),
assigned_here_msg.c_str ());
}
rust_error_at (r, ErrorCode::E0081, "%s",
assigned_more_once_msg.c_str ());
}
return !has_failed;
}
void
TyTyResolveCompile::visit (const TyTy::ADTType &type)
{
tree type_record = error_mark_node;
if (!type.is_enum ())
{
rust_assert (type.number_of_variants () == 1);

TyTy::VariantDef &variant = *type.get_variants ().at (0);
std::vector<Backend::typed_identifier> fields;
for (size_t i = 0; i < variant.num_fields (); i++)
Expand Down Expand Up @@ -349,9 +405,15 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
// add them to the list
variant_records.push_back (named_variant_record);
}

// now we need to make the actual union, but first we need to make
// named_type TYPE_DECL's out of the variants
// TODO: bdbt set up defid and a map (or set?) to check if we have
// checked for collision already.
if (!check_variant_record_collision (ctx, type, variant_records))
{
translated = error_mark_node;
return;
}
// the actual union, but first we need to make named_type TYPE_DECL's out
// of the variants

size_t i = 0;
std::vector<Backend::typed_identifier> enum_fields;
Expand Down
13 changes: 10 additions & 3 deletions gcc/rust/rust-diagnostics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ expand_format (const char *fmt)
// calling function must need to have attribute gnu_printf as well, even
// though there is already an attribute declaration for it.

static std::string
expand_message (const char *fmt, va_list ap) RUST_ATTRIBUTE_GCC_DIAG (1, 0);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"

Expand All @@ -127,6 +124,16 @@ expand_message (const char *fmt, va_list ap)
free (mbuf);
return rval;
}
std::string
expand_message (const char *fmt, ...)
{
va_list ap;

va_start (ap, fmt);
std::string str = expand_message (fmt, ap);
va_end (ap);
return str;
}

#pragma GCC diagnostic pop

Expand Down
5 changes: 4 additions & 1 deletion gcc/rust/rust-diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
#define RUST_ATTRIBUTE_GCC_DIAG(m, n) \
__attribute__ ((__format__ (__gcc_tdiag__, m, n))) \
__attribute__ ((__nonnull__ (m)))
__attribute__ ((__nonnull__ (m)))
#else
#define RUST_ATTRIBUTE_GCC_DIAG(m, n)
#endif
Expand Down Expand Up @@ -80,6 +80,9 @@ enum class ErrorCode : unsigned int
#undef ERROR

// clang-format off
std::string
expand_message (const char *fmt, ...) RUST_ATTRIBUTE_GCC_DIAG (1, 2);

extern void
rust_internal_error_at (const location_t, const char *fmt, ...)
RUST_ATTRIBUTE_GCC_DIAG (2, 3)
Expand Down
7 changes: 7 additions & 0 deletions gcc/testsuite/rust/compile/shared_discriminator_enum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// from issue 3351: https://github.com/Rust-GCC/gccrs/issues/3351
enum Foo { // { dg-error "discriminant value 1 assigned more than once" }
Bar = 1,
Qaz = 1,
}

fn main() {}