|
20 | 20 | #include "rust-compile-expr.h"
|
21 | 21 | #include "rust-constexpr.h"
|
22 | 22 | #include "rust-gcc.h"
|
| 23 | +#include "rust-diagnostics.h" |
23 | 24 |
|
24 | 25 | #include "tree.h"
|
25 | 26 | #include "stor-layout.h"
|
26 |
| - |
27 | 27 | namespace Rust {
|
28 | 28 | namespace Compile {
|
29 | 29 |
|
@@ -248,14 +248,72 @@ TyTyResolveCompile::visit (const TyTy::FnPtr &type)
|
248 | 248 | type.get_ident ().locus);
|
249 | 249 | }
|
250 | 250 |
|
| 251 | +bool |
| 252 | +check_variant_record_collision (Context *ctx, const TyTy::ADTType &type, |
| 253 | + std::vector<tree> &variant_records) |
| 254 | +{ |
| 255 | + // bdbt: we're checking if shared discriminants crash with each other or |
| 256 | + // not. lets make a map from uhwi to hir id. A clash of uhwi in a variant |
| 257 | + // record to which said record can be converted uhwi is indicative of |
| 258 | + // issue 3351 of gccrs |
| 259 | + |
| 260 | + std::map<HOST_WIDE_INT, std::vector<size_t>> shwi_to_index; |
| 261 | + for (size_t i = 0; i < variant_records.size (); i++) |
| 262 | + { |
| 263 | + TyTy::VariantDef *variant = type.get_variants ().at (i); |
| 264 | + if (variant->has_discriminant ()) |
| 265 | + { |
| 266 | + ctx->push_const_context (); |
| 267 | + tree discriminant_expr |
| 268 | + = CompileExpr::Compile (variant->get_discriminant (), ctx); |
| 269 | + ctx->push_const_context (); |
| 270 | + tree folded_expr = fold_expr (discriminant_expr); |
| 271 | + if (folded_expr == error_mark_node) |
| 272 | + { |
| 273 | + // if we have discriminant but we fail to fold it, return false |
| 274 | + return false; |
| 275 | + } |
| 276 | + HOST_WIDE_INT discriminant_integer = tree_to_shwi (folded_expr); |
| 277 | + shwi_to_index[discriminant_integer].push_back (i); |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + bool has_failed = false; |
| 282 | + for (const auto &map_item : shwi_to_index) |
| 283 | + { |
| 284 | + auto discriminant_integer = map_item.first; |
| 285 | + const auto &index_vector = map_item.second; |
| 286 | + // collision doesn't happen, move to next item |
| 287 | + if (index_vector.size () <= 1) |
| 288 | + continue; |
| 289 | + |
| 290 | + has_failed = true; |
| 291 | + rich_location r (line_table, type.get_locus ()); |
| 292 | + std::string assigned_here_msg |
| 293 | + = expand_message (HOST_WIDE_INT_PRINT_DEC " assigned here", |
| 294 | + discriminant_integer); |
| 295 | + std::string assigned_more_once_msg |
| 296 | + = expand_message ("discriminant value " HOST_WIDE_INT_PRINT_DEC |
| 297 | + " assigned more than once", |
| 298 | + discriminant_integer); |
| 299 | + for (auto index : index_vector) |
| 300 | + { |
| 301 | + TyTy::VariantDef *variant = type.get_variants ().at (index); |
| 302 | + r.add_fixit_replace (variant->get_discriminant ().get_locus (), |
| 303 | + assigned_here_msg.c_str ()); |
| 304 | + } |
| 305 | + rust_error_at (r, ErrorCode::E0081, "%s", |
| 306 | + assigned_more_once_msg.c_str ()); |
| 307 | + } |
| 308 | + return !has_failed; |
| 309 | +} |
251 | 310 | void
|
252 | 311 | TyTyResolveCompile::visit (const TyTy::ADTType &type)
|
253 | 312 | {
|
254 | 313 | tree type_record = error_mark_node;
|
255 | 314 | if (!type.is_enum ())
|
256 | 315 | {
|
257 | 316 | rust_assert (type.number_of_variants () == 1);
|
258 |
| - |
259 | 317 | TyTy::VariantDef &variant = *type.get_variants ().at (0);
|
260 | 318 | std::vector<Backend::typed_identifier> fields;
|
261 | 319 | for (size_t i = 0; i < variant.num_fields (); i++)
|
@@ -358,9 +416,15 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
|
358 | 416 | // add them to the list
|
359 | 417 | variant_records.push_back (named_variant_record);
|
360 | 418 | }
|
361 |
| - |
362 |
| - // now we need to make the actual union, but first we need to make |
363 |
| - // named_type TYPE_DECL's out of the variants |
| 419 | + // TODO: bdbt set up defid and a map (or set?) to check if we have |
| 420 | + // checked for collision already. |
| 421 | + if (!check_variant_record_collision (ctx, type, variant_records)) |
| 422 | + { |
| 423 | + translated = error_mark_node; |
| 424 | + return; |
| 425 | + } |
| 426 | + // the actual union, but first we need to make named_type TYPE_DECL's out |
| 427 | + // of the variants |
364 | 428 |
|
365 | 429 | size_t i = 0;
|
366 | 430 | std::vector<Backend::typed_identifier> enum_fields;
|
|
0 commit comments