|
23 | 23 |
|
24 | 24 | #include "tree.h"
|
25 | 25 | #include "stor-layout.h"
|
26 |
| - |
27 | 26 | namespace Rust {
|
28 | 27 | namespace Compile {
|
29 | 28 |
|
@@ -239,14 +238,69 @@ TyTyResolveCompile::visit (const TyTy::FnPtr &type)
|
239 | 238 | type.get_ident ().locus);
|
240 | 239 | }
|
241 | 240 |
|
| 241 | +bool |
| 242 | +check_variant_record_collision (Context *ctx, const TyTy::ADTType &type, |
| 243 | + std::vector<tree> &variant_records) |
| 244 | +{ |
| 245 | + // bdbt: we're checking if shared discriminants crash with each other or |
| 246 | + // not. lets make a map from uhwi to hir id. A clash of uhwi in a variant |
| 247 | + // record to which said record can be converted uhwi is indicative of |
| 248 | + // issue 3351 of gccrs |
| 249 | + |
| 250 | + std::map<HOST_WIDE_INT, std::vector<size_t>> shwi_to_index; |
| 251 | + for (size_t i = 0; i < variant_records.size (); i++) |
| 252 | + { |
| 253 | + TyTy::VariantDef *variant = type.get_variants ().at (i); |
| 254 | + if (variant->has_discriminant ()) |
| 255 | + { |
| 256 | + tree discriminant_expr |
| 257 | + = CompileExpr::Compile (variant->get_discriminant (), ctx); |
| 258 | + tree folded_expr = fold_expr (discriminant_expr); |
| 259 | + if (folded_expr == error_mark_node) |
| 260 | + { |
| 261 | + // if we have discriminant but we fail to fold it, return false |
| 262 | + return false; |
| 263 | + } |
| 264 | + HOST_WIDE_INT discriminant_integer = tree_to_shwi (folded_expr); |
| 265 | + shwi_to_index[discriminant_integer].push_back (i); |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + bool has_failed = false; |
| 270 | + for (const auto &map_item : shwi_to_index) |
| 271 | + { |
| 272 | + auto discriminant_integer = map_item.first; |
| 273 | + const auto &index_vector = map_item.second; |
| 274 | + // collision doesn't happen, move to next item |
| 275 | + if (index_vector.size () <= 1) |
| 276 | + continue; |
| 277 | + |
| 278 | + has_failed = true; |
| 279 | + rich_location r (line_table, type.get_locus ()); |
| 280 | + std::string assigned_here_msg = "\"" |
| 281 | + + std::to_string (discriminant_integer) |
| 282 | + + "\"" + " assigned here"; |
| 283 | + std::string assigned_more_once_msg |
| 284 | + = "discriminant value \"" + std::to_string (discriminant_integer) + "\"" |
| 285 | + + " assigned more than once"; |
| 286 | + for (auto index : index_vector) |
| 287 | + { |
| 288 | + TyTy::VariantDef *variant = type.get_variants ().at (index); |
| 289 | + r.add_fixit_replace (variant->get_discriminant ().get_locus (), |
| 290 | + assigned_here_msg.c_str ()); |
| 291 | + } |
| 292 | + rust_error_at (r, ErrorCode::E0081, "%s", |
| 293 | + assigned_more_once_msg.c_str ()); |
| 294 | + } |
| 295 | + return !has_failed; |
| 296 | +} |
242 | 297 | void
|
243 | 298 | TyTyResolveCompile::visit (const TyTy::ADTType &type)
|
244 | 299 | {
|
245 | 300 | tree type_record = error_mark_node;
|
246 | 301 | if (!type.is_enum ())
|
247 | 302 | {
|
248 | 303 | rust_assert (type.number_of_variants () == 1);
|
249 |
| - |
250 | 304 | TyTy::VariantDef &variant = *type.get_variants ().at (0);
|
251 | 305 | std::vector<Backend::typed_identifier> fields;
|
252 | 306 | for (size_t i = 0; i < variant.num_fields (); i++)
|
@@ -349,9 +403,15 @@ TyTyResolveCompile::visit (const TyTy::ADTType &type)
|
349 | 403 | // add them to the list
|
350 | 404 | variant_records.push_back (named_variant_record);
|
351 | 405 | }
|
352 |
| - |
353 |
| - // now we need to make the actual union, but first we need to make |
354 |
| - // named_type TYPE_DECL's out of the variants |
| 406 | + // TODO: bdbt set up defid and a map (or set?) to check if we have |
| 407 | + // checked for collision already. |
| 408 | + if (!check_variant_record_collision (ctx, type, variant_records)) |
| 409 | + { |
| 410 | + translated = error_mark_node; |
| 411 | + return; |
| 412 | + } |
| 413 | + // the actual union, but first we need to make named_type TYPE_DECL's out |
| 414 | + // of the variants |
355 | 415 |
|
356 | 416 | size_t i = 0;
|
357 | 417 | std::vector<Backend::typed_identifier> enum_fields;
|
|
0 commit comments