| 1 |
// Copyright (C) 2020-2024 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
// This file is part of GCC. |
| 4 |
|
| 5 |
// GCC is free software; you can redistribute it and/or modify it under |
| 6 |
// the terms of the GNU General Public License as published by the Free |
| 7 |
// Software Foundation; either version 3, or (at your option) any later |
| 8 |
// version. |
| 9 |
|
| 10 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
// for more details. |
| 14 |
|
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with GCC; see the file COPYING3. If not see |
| 17 |
// <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
#include "rust-system.h" |
| 20 |
#include "rust-hir-pattern-analysis.h" |
| 21 |
#include "rust-diagnostics.h" |
| 22 |
#include "rust-hir-full-decls.h" |
| 23 |
#include "rust-hir-path.h" |
| 24 |
#include "rust-hir-pattern.h" |
| 25 |
#include "rust-hir.h" |
| 26 |
#include "rust-mapping-common.h" |
| 27 |
#include "rust-system.h" |
| 28 |
#include "rust-tyty.h" |
| 29 |
#include "rust-immutable-name-resolution-context.h" |
| 30 |
|
| 31 |
// for flag_name_resolution_2_0 |
| 32 |
#include "options.h" |
| 33 |
|
| 34 |
namespace Rust { |
| 35 |
namespace Analysis { |
| 36 |
|
| 37 |
PatternChecker::PatternChecker () |
| 38 |
: tyctx (*Resolver::TypeCheckContext::get ()), |
| 39 |
resolver (*Resolver::Resolver::get ()), |
| 40 |
mappings (Analysis::Mappings::get ()) |
| 41 |
{} |
| 42 |
|
| 43 |
void |
| 44 |
PatternChecker::go (HIR::Crate &crate) |
| 45 |
{ |
| 46 |
rust_debug ("started pattern check"); |
| 47 |
for (auto &item : crate.get_items ()) |
| 48 |
item->accept_vis (*this); |
| 49 |
rust_debug ("finished pattern check"); |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
PatternChecker::visit (Lifetime &) |
| 54 |
{} |
| 55 |
|
| 56 |
void |
| 57 |
PatternChecker::visit (LifetimeParam &) |
| 58 |
{} |
| 59 |
|
| 60 |
void |
| 61 |
PatternChecker::visit (PathInExpression &path) |
| 62 |
{} |
| 63 |
|
| 64 |
void |
| 65 |
PatternChecker::visit (TypePathSegment &) |
| 66 |
{} |
| 67 |
|
| 68 |
void |
| 69 |
PatternChecker::visit (TypePathSegmentGeneric &) |
| 70 |
{} |
| 71 |
|
| 72 |
void |
| 73 |
PatternChecker::visit (TypePathSegmentFunction &) |
| 74 |
{} |
| 75 |
|
| 76 |
void |
| 77 |
PatternChecker::visit (TypePath &) |
| 78 |
{} |
| 79 |
|
| 80 |
void |
| 81 |
PatternChecker::visit (QualifiedPathInExpression &) |
| 82 |
{} |
| 83 |
|
| 84 |
void |
| 85 |
PatternChecker::visit (QualifiedPathInType &) |
| 86 |
{} |
| 87 |
|
| 88 |
void |
| 89 |
PatternChecker::visit (LiteralExpr &) |
| 90 |
{} |
| 91 |
|
| 92 |
void |
| 93 |
PatternChecker::visit (BorrowExpr &expr) |
| 94 |
{ |
| 95 |
expr.get_expr ().accept_vis (*this); |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
PatternChecker::visit (DereferenceExpr &expr) |
| 100 |
{ |
| 101 |
expr.get_expr ().accept_vis (*this); |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
PatternChecker::visit (ErrorPropagationExpr &expr) |
| 106 |
{ |
| 107 |
expr.get_expr ().accept_vis (*this); |
| 108 |
} |
| 109 |
|
| 110 |
void |
| 111 |
PatternChecker::visit (NegationExpr &expr) |
| 112 |
{ |
| 113 |
expr.get_expr ().accept_vis (*this); |
| 114 |
} |
| 115 |
|
| 116 |
void |
| 117 |
PatternChecker::visit (ArithmeticOrLogicalExpr &expr) |
| 118 |
{ |
| 119 |
expr.get_lhs ().accept_vis (*this); |
| 120 |
expr.get_rhs ().accept_vis (*this); |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
PatternChecker::visit (ComparisonExpr &expr) |
| 125 |
{ |
| 126 |
expr.get_lhs ().accept_vis (*this); |
| 127 |
expr.get_rhs ().accept_vis (*this); |
| 128 |
} |
| 129 |
|
| 130 |
void |
| 131 |
PatternChecker::visit (LazyBooleanExpr &expr) |
| 132 |
{ |
| 133 |
expr.get_lhs ().accept_vis (*this); |
| 134 |
expr.get_rhs ().accept_vis (*this); |
| 135 |
} |
| 136 |
|
| 137 |
void |
| 138 |
PatternChecker::visit (TypeCastExpr &expr) |
| 139 |
{ |
| 140 |
expr.get_expr ().accept_vis (*this); |
| 141 |
} |
| 142 |
|
| 143 |
void |
| 144 |
PatternChecker::visit (AssignmentExpr &expr) |
| 145 |
{ |
| 146 |
expr.get_lhs ().accept_vis (*this); |
| 147 |
expr.get_rhs ().accept_vis (*this); |
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
PatternChecker::visit (CompoundAssignmentExpr &expr) |
| 152 |
{ |
| 153 |
expr.get_lhs ().accept_vis (*this); |
| 154 |
expr.get_rhs ().accept_vis (*this); |
| 155 |
} |
| 156 |
|
| 157 |
void |
| 158 |
PatternChecker::visit (GroupedExpr &expr) |
| 159 |
{ |
| 160 |
expr.get_expr_in_parens ().accept_vis (*this); |
| 161 |
} |
| 162 |
|
| 163 |
void |
| 164 |
PatternChecker::visit (ArrayElemsValues &elems) |
| 165 |
{ |
| 166 |
for (auto &elem : elems.get_values ()) |
| 167 |
elem->accept_vis (*this); |
| 168 |
} |
| 169 |
|
| 170 |
void |
| 171 |
PatternChecker::visit (ArrayElemsCopied &elems) |
| 172 |
{ |
| 173 |
elems.get_elem_to_copy ().accept_vis (*this); |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
PatternChecker::visit (ArrayExpr &expr) |
| 178 |
{ |
| 179 |
expr.get_internal_elements ().accept_vis (*this); |
| 180 |
} |
| 181 |
|
| 182 |
void |
| 183 |
PatternChecker::visit (ArrayIndexExpr &expr) |
| 184 |
{ |
| 185 |
expr.get_array_expr ().accept_vis (*this); |
| 186 |
expr.get_index_expr ().accept_vis (*this); |
| 187 |
} |
| 188 |
|
| 189 |
void |
| 190 |
PatternChecker::visit (TupleExpr &expr) |
| 191 |
{ |
| 192 |
for (auto &elem : expr.get_tuple_elems ()) |
| 193 |
elem->accept_vis (*this); |
| 194 |
} |
| 195 |
|
| 196 |
void |
| 197 |
PatternChecker::visit (TupleIndexExpr &expr) |
| 198 |
{ |
| 199 |
expr.get_tuple_expr ().accept_vis (*this); |
| 200 |
} |
| 201 |
|
| 202 |
void |
| 203 |
PatternChecker::visit (StructExprStruct &) |
| 204 |
{} |
| 205 |
|
| 206 |
void |
| 207 |
PatternChecker::visit (StructExprFieldIdentifier &) |
| 208 |
{} |
| 209 |
|
| 210 |
void |
| 211 |
PatternChecker::visit (StructExprFieldIdentifierValue &field) |
| 212 |
{ |
| 213 |
field.get_value ().accept_vis (*this); |
| 214 |
} |
| 215 |
|
| 216 |
void |
| 217 |
PatternChecker::visit (StructExprFieldIndexValue &field) |
| 218 |
{ |
| 219 |
field.get_value ().accept_vis (*this); |
| 220 |
} |
| 221 |
|
| 222 |
void |
| 223 |
PatternChecker::visit (StructExprStructFields &expr) |
| 224 |
{ |
| 225 |
for (auto &field : expr.get_fields ()) |
| 226 |
field->accept_vis (*this); |
| 227 |
} |
| 228 |
|
| 229 |
void |
| 230 |
PatternChecker::visit (StructExprStructBase &) |
| 231 |
{} |
| 232 |
|
| 233 |
void |
| 234 |
PatternChecker::visit (CallExpr &expr) |
| 235 |
{ |
| 236 |
if (!expr.has_fnexpr ()) |
| 237 |
return; |
| 238 |
|
| 239 |
NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid (); |
| 240 |
NodeId ref_node_id; |
| 241 |
if (flag_name_resolution_2_0) |
| 242 |
{ |
| 243 |
auto &nr_ctx |
| 244 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 245 |
|
| 246 |
if (auto id = nr_ctx.lookup (ast_node_id)) |
| 247 |
ref_node_id = *id; |
| 248 |
else |
| 249 |
return; |
| 250 |
} |
| 251 |
else if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id)) |
| 252 |
return; |
| 253 |
|
| 254 |
if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id)) |
| 255 |
{ |
| 256 |
if (expr.has_params ()) |
| 257 |
for (auto &arg : expr.get_arguments ()) |
| 258 |
arg->accept_vis (*this); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
rust_unreachable (); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
PatternChecker::visit (MethodCallExpr &expr) |
| 268 |
{ |
| 269 |
expr.get_receiver ().accept_vis (*this); |
| 270 |
|
| 271 |
for (auto &arg : expr.get_arguments ()) |
| 272 |
arg->accept_vis (*this); |
| 273 |
} |
| 274 |
|
| 275 |
void |
| 276 |
PatternChecker::visit (FieldAccessExpr &expr) |
| 277 |
{ |
| 278 |
expr.get_receiver_expr ().accept_vis (*this); |
| 279 |
} |
| 280 |
|
| 281 |
void |
| 282 |
PatternChecker::visit (ClosureExpr &expr) |
| 283 |
{ |
| 284 |
expr.get_expr ().accept_vis (*this); |
| 285 |
} |
| 286 |
|
| 287 |
void |
| 288 |
PatternChecker::visit (BlockExpr &expr) |
| 289 |
{ |
| 290 |
for (auto &stmt : expr.get_statements ()) |
| 291 |
stmt->accept_vis (*this); |
| 292 |
|
| 293 |
if (expr.has_expr ()) |
| 294 |
expr.get_final_expr ().accept_vis (*this); |
| 295 |
} |
| 296 |
|
| 297 |
void |
| 298 |
PatternChecker::visit (ContinueExpr &) |
| 299 |
{} |
| 300 |
|
| 301 |
void |
| 302 |
PatternChecker::visit (BreakExpr &expr) |
| 303 |
{ |
| 304 |
if (expr.has_break_expr ()) |
| 305 |
expr.get_expr ().accept_vis (*this); |
| 306 |
} |
| 307 |
|
| 308 |
void |
| 309 |
PatternChecker::visit (RangeFromToExpr &expr) |
| 310 |
{ |
| 311 |
expr.get_from_expr ().accept_vis (*this); |
| 312 |
expr.get_to_expr ().accept_vis (*this); |
| 313 |
} |
| 314 |
|
| 315 |
void |
| 316 |
PatternChecker::visit (RangeFromExpr &expr) |
| 317 |
{ |
| 318 |
expr.get_from_expr ().accept_vis (*this); |
| 319 |
} |
| 320 |
|
| 321 |
void |
| 322 |
PatternChecker::visit (RangeToExpr &expr) |
| 323 |
{ |
| 324 |
expr.get_to_expr ().accept_vis (*this); |
| 325 |
} |
| 326 |
|
| 327 |
void |
| 328 |
PatternChecker::visit (RangeFullExpr &) |
| 329 |
{} |
| 330 |
|
| 331 |
void |
| 332 |
PatternChecker::visit (RangeFromToInclExpr &expr) |
| 333 |
{ |
| 334 |
expr.get_from_expr ().accept_vis (*this); |
| 335 |
expr.get_to_expr ().accept_vis (*this); |
| 336 |
} |
| 337 |
|
| 338 |
void |
| 339 |
PatternChecker::visit (RangeToInclExpr &expr) |
| 340 |
{ |
| 341 |
expr.get_to_expr ().accept_vis (*this); |
| 342 |
} |
| 343 |
|
| 344 |
void |
| 345 |
PatternChecker::visit (ReturnExpr &expr) |
| 346 |
{ |
| 347 |
if (expr.has_return_expr ()) |
| 348 |
expr.get_expr ().accept_vis (*this); |
| 349 |
} |
| 350 |
|
| 351 |
void |
| 352 |
PatternChecker::visit (UnsafeBlockExpr &expr) |
| 353 |
{ |
| 354 |
expr.get_block_expr ().accept_vis (*this); |
| 355 |
} |
| 356 |
|
| 357 |
void |
| 358 |
PatternChecker::visit (LoopExpr &expr) |
| 359 |
{ |
| 360 |
expr.get_loop_block ().accept_vis (*this); |
| 361 |
} |
| 362 |
|
| 363 |
void |
| 364 |
PatternChecker::visit (WhileLoopExpr &expr) |
| 365 |
{ |
| 366 |
expr.get_predicate_expr ().accept_vis (*this); |
| 367 |
expr.get_loop_block ().accept_vis (*this); |
| 368 |
} |
| 369 |
|
| 370 |
void |
| 371 |
PatternChecker::visit (WhileLetLoopExpr &expr) |
| 372 |
{ |
| 373 |
expr.get_cond ().accept_vis (*this); |
| 374 |
expr.get_loop_block ().accept_vis (*this); |
| 375 |
} |
| 376 |
|
| 377 |
void |
| 378 |
PatternChecker::visit (IfExpr &expr) |
| 379 |
{ |
| 380 |
expr.get_if_condition ().accept_vis (*this); |
| 381 |
expr.get_if_block ().accept_vis (*this); |
| 382 |
} |
| 383 |
|
| 384 |
void |
| 385 |
PatternChecker::visit (IfExprConseqElse &expr) |
| 386 |
{ |
| 387 |
expr.get_if_condition ().accept_vis (*this); |
| 388 |
expr.get_if_block ().accept_vis (*this); |
| 389 |
expr.get_else_block ().accept_vis (*this); |
| 390 |
} |
| 391 |
|
| 392 |
void |
| 393 |
PatternChecker::visit (MatchExpr &expr) |
| 394 |
{ |
| 395 |
expr.get_scrutinee_expr ().accept_vis (*this); |
| 396 |
|
| 397 |
for (auto &match_arm : expr.get_match_cases ()) |
| 398 |
match_arm.get_expr ().accept_vis (*this); |
| 399 |
|
| 400 |
// match expressions are only an entrypoint |
| 401 |
TyTy::BaseType *scrutinee_ty; |
| 402 |
bool ok = tyctx.lookup_type ( |
| 403 |
expr.get_scrutinee_expr ().get_mappings ().get_hirid (), &scrutinee_ty); |
| 404 |
rust_assert (ok); |
| 405 |
|
| 406 |
check_match_usefulness (&tyctx, scrutinee_ty, expr); |
| 407 |
} |
| 408 |
|
| 409 |
void |
| 410 |
PatternChecker::visit (AwaitExpr &) |
| 411 |
{ |
| 412 |
// TODO: Visit expression |
| 413 |
} |
| 414 |
|
| 415 |
void |
| 416 |
PatternChecker::visit (AsyncBlockExpr &) |
| 417 |
{ |
| 418 |
// TODO: Visit block expression |
| 419 |
} |
| 420 |
|
| 421 |
void |
| 422 |
PatternChecker::visit (InlineAsm &expr) |
| 423 |
{} |
| 424 |
|
| 425 |
void |
| 426 |
PatternChecker::visit (TypeParam &) |
| 427 |
{} |
| 428 |
|
| 429 |
void |
| 430 |
PatternChecker::visit (ConstGenericParam &) |
| 431 |
{} |
| 432 |
|
| 433 |
void |
| 434 |
PatternChecker::visit (LifetimeWhereClauseItem &) |
| 435 |
{} |
| 436 |
|
| 437 |
void |
| 438 |
PatternChecker::visit (TypeBoundWhereClauseItem &) |
| 439 |
{} |
| 440 |
|
| 441 |
void |
| 442 |
PatternChecker::visit (Module &module) |
| 443 |
{ |
| 444 |
for (auto &item : module.get_items ()) |
| 445 |
item->accept_vis (*this); |
| 446 |
} |
| 447 |
|
| 448 |
void |
| 449 |
PatternChecker::visit (ExternCrate &) |
| 450 |
{} |
| 451 |
|
| 452 |
void |
| 453 |
PatternChecker::visit (UseTreeGlob &) |
| 454 |
{} |
| 455 |
|
| 456 |
void |
| 457 |
PatternChecker::visit (UseTreeList &) |
| 458 |
{} |
| 459 |
|
| 460 |
void |
| 461 |
PatternChecker::visit (UseTreeRebind &) |
| 462 |
{} |
| 463 |
|
| 464 |
void |
| 465 |
PatternChecker::visit (UseDeclaration &) |
| 466 |
{} |
| 467 |
|
| 468 |
void |
| 469 |
PatternChecker::visit (Function &function) |
| 470 |
{ |
| 471 |
function.get_definition ().accept_vis (*this); |
| 472 |
} |
| 473 |
|
| 474 |
void |
| 475 |
PatternChecker::visit (TypeAlias &) |
| 476 |
{} |
| 477 |
|
| 478 |
void |
| 479 |
PatternChecker::visit (StructStruct &) |
| 480 |
{} |
| 481 |
|
| 482 |
void |
| 483 |
PatternChecker::visit (TupleStruct &) |
| 484 |
{} |
| 485 |
|
| 486 |
void |
| 487 |
PatternChecker::visit (EnumItem &) |
| 488 |
{} |
| 489 |
|
| 490 |
void |
| 491 |
PatternChecker::visit (EnumItemTuple &) |
| 492 |
{} |
| 493 |
|
| 494 |
void |
| 495 |
PatternChecker::visit (EnumItemStruct &) |
| 496 |
{} |
| 497 |
|
| 498 |
void |
| 499 |
PatternChecker::visit (EnumItemDiscriminant &) |
| 500 |
{} |
| 501 |
|
| 502 |
void |
| 503 |
PatternChecker::visit (Enum &) |
| 504 |
{} |
| 505 |
|
| 506 |
void |
| 507 |
PatternChecker::visit (Union &) |
| 508 |
{} |
| 509 |
|
| 510 |
void |
| 511 |
PatternChecker::visit (ConstantItem &const_item) |
| 512 |
{ |
| 513 |
const_item.get_expr ().accept_vis (*this); |
| 514 |
} |
| 515 |
|
| 516 |
void |
| 517 |
PatternChecker::visit (StaticItem &static_item) |
| 518 |
{ |
| 519 |
static_item.get_expr ().accept_vis (*this); |
| 520 |
} |
| 521 |
|
| 522 |
void |
| 523 |
PatternChecker::visit (TraitItemFunc &item) |
| 524 |
{ |
| 525 |
if (item.has_definition ()) |
| 526 |
item.get_block_expr ().accept_vis (*this); |
| 527 |
} |
| 528 |
|
| 529 |
void |
| 530 |
PatternChecker::visit (TraitItemConst &item) |
| 531 |
{ |
| 532 |
if (item.has_expr ()) |
| 533 |
item.get_expr ().accept_vis (*this); |
| 534 |
} |
| 535 |
|
| 536 |
void |
| 537 |
PatternChecker::visit (TraitItemType &) |
| 538 |
{} |
| 539 |
|
| 540 |
void |
| 541 |
PatternChecker::visit (Trait &trait) |
| 542 |
{ |
| 543 |
for (auto &item : trait.get_trait_items ()) |
| 544 |
item->accept_vis (*this); |
| 545 |
} |
| 546 |
|
| 547 |
void |
| 548 |
PatternChecker::visit (ImplBlock &impl) |
| 549 |
{ |
| 550 |
for (auto &item : impl.get_impl_items ()) |
| 551 |
item->accept_vis (*this); |
| 552 |
} |
| 553 |
|
| 554 |
void |
| 555 |
PatternChecker::visit (ExternalStaticItem &) |
| 556 |
{} |
| 557 |
|
| 558 |
void |
| 559 |
PatternChecker::visit (ExternalFunctionItem &) |
| 560 |
{} |
| 561 |
|
| 562 |
void |
| 563 |
PatternChecker::visit (ExternalTypeItem &) |
| 564 |
{} |
| 565 |
|
| 566 |
void |
| 567 |
PatternChecker::visit (ExternBlock &block) |
| 568 |
{ |
| 569 |
// FIXME: Do we need to do this? |
| 570 |
for (auto &item : block.get_extern_items ()) |
| 571 |
item->accept_vis (*this); |
| 572 |
} |
| 573 |
|
| 574 |
void |
| 575 |
PatternChecker::visit (LiteralPattern &) |
| 576 |
{} |
| 577 |
|
| 578 |
void |
| 579 |
PatternChecker::visit (IdentifierPattern &) |
| 580 |
{} |
| 581 |
|
| 582 |
void |
| 583 |
PatternChecker::visit (WildcardPattern &) |
| 584 |
{} |
| 585 |
|
| 586 |
void |
| 587 |
PatternChecker::visit (RangePatternBoundLiteral &) |
| 588 |
{} |
| 589 |
|
| 590 |
void |
| 591 |
PatternChecker::visit (RangePatternBoundPath &) |
| 592 |
{} |
| 593 |
|
| 594 |
void |
| 595 |
PatternChecker::visit (RangePatternBoundQualPath &) |
| 596 |
{} |
| 597 |
|
| 598 |
void |
| 599 |
PatternChecker::visit (RangePattern &) |
| 600 |
{} |
| 601 |
|
| 602 |
void |
| 603 |
PatternChecker::visit (ReferencePattern &) |
| 604 |
{} |
| 605 |
|
| 606 |
void |
| 607 |
PatternChecker::visit (StructPatternFieldTuplePat &) |
| 608 |
{} |
| 609 |
|
| 610 |
void |
| 611 |
PatternChecker::visit (StructPatternFieldIdentPat &) |
| 612 |
{} |
| 613 |
|
| 614 |
void |
| 615 |
PatternChecker::visit (StructPatternFieldIdent &) |
| 616 |
{} |
| 617 |
|
| 618 |
void |
| 619 |
PatternChecker::visit (StructPattern &) |
| 620 |
{} |
| 621 |
|
| 622 |
void |
| 623 |
PatternChecker::visit (TupleStructItemsNoRange &) |
| 624 |
{} |
| 625 |
|
| 626 |
void |
| 627 |
PatternChecker::visit (TupleStructItemsRange &) |
| 628 |
{} |
| 629 |
|
| 630 |
void |
| 631 |
PatternChecker::visit (TupleStructPattern &) |
| 632 |
{} |
| 633 |
|
| 634 |
void |
| 635 |
PatternChecker::visit (TuplePatternItemsMultiple &) |
| 636 |
{} |
| 637 |
|
| 638 |
void |
| 639 |
PatternChecker::visit (TuplePatternItemsRanged &) |
| 640 |
{} |
| 641 |
|
| 642 |
void |
| 643 |
PatternChecker::visit (TuplePattern &) |
| 644 |
{} |
| 645 |
|
| 646 |
void |
| 647 |
PatternChecker::visit (SlicePattern &) |
| 648 |
{} |
| 649 |
|
| 650 |
void |
| 651 |
PatternChecker::visit (AltPattern &) |
| 652 |
{} |
| 653 |
|
| 654 |
void |
| 655 |
PatternChecker::visit (EmptyStmt &) |
| 656 |
{} |
| 657 |
|
| 658 |
void |
| 659 |
PatternChecker::visit (LetStmt &stmt) |
| 660 |
{ |
| 661 |
if (stmt.has_init_expr ()) |
| 662 |
stmt.get_init_expr ().accept_vis (*this); |
| 663 |
} |
| 664 |
|
| 665 |
void |
| 666 |
PatternChecker::visit (ExprStmt &stmt) |
| 667 |
{ |
| 668 |
stmt.get_expr ().accept_vis (*this); |
| 669 |
} |
| 670 |
|
| 671 |
void |
| 672 |
PatternChecker::visit (TraitBound &) |
| 673 |
{} |
| 674 |
|
| 675 |
void |
| 676 |
PatternChecker::visit (ImplTraitType &) |
| 677 |
{} |
| 678 |
|
| 679 |
void |
| 680 |
PatternChecker::visit (TraitObjectType &) |
| 681 |
{} |
| 682 |
|
| 683 |
void |
| 684 |
PatternChecker::visit (ParenthesisedType &) |
| 685 |
{} |
| 686 |
|
| 687 |
void |
| 688 |
PatternChecker::visit (TupleType &) |
| 689 |
{} |
| 690 |
|
| 691 |
void |
| 692 |
PatternChecker::visit (NeverType &) |
| 693 |
{} |
| 694 |
|
| 695 |
void |
| 696 |
PatternChecker::visit (RawPointerType &) |
| 697 |
{} |
| 698 |
|
| 699 |
void |
| 700 |
PatternChecker::visit (ReferenceType &) |
| 701 |
{} |
| 702 |
|
| 703 |
void |
| 704 |
PatternChecker::visit (ArrayType &) |
| 705 |
{} |
| 706 |
|
| 707 |
void |
| 708 |
PatternChecker::visit (SliceType &) |
| 709 |
{} |
| 710 |
|
| 711 |
void |
| 712 |
PatternChecker::visit (InferredType &) |
| 713 |
{} |
| 714 |
|
| 715 |
void |
| 716 |
PatternChecker::visit (BareFunctionType &) |
| 717 |
{} |
| 718 |
|
| 719 |
bool |
| 720 |
Constructor::is_covered_by (const Constructor &o) const |
| 721 |
{ |
| 722 |
if (o.kind == ConstructorKind::WILDCARD) |
| 723 |
return true; |
| 724 |
|
| 725 |
switch (kind) |
| 726 |
{ |
| 727 |
case ConstructorKind::VARIANT: { |
| 728 |
rust_assert (kind == ConstructorKind::VARIANT); |
| 729 |
return variant_idx == o.variant_idx; |
| 730 |
} |
| 731 |
break; |
| 732 |
case ConstructorKind::INT_RANGE: { |
| 733 |
rust_assert (kind == ConstructorKind::INT_RANGE); |
| 734 |
return int_range.lo >= o.int_range.lo && int_range.hi <= o.int_range.hi; |
| 735 |
} |
| 736 |
break; |
| 737 |
case ConstructorKind::WILDCARD: { |
| 738 |
// TODO: wildcard is covered by a variant of enum with a single |
| 739 |
// variant |
| 740 |
return false; |
| 741 |
} |
| 742 |
break; |
| 743 |
case ConstructorKind::STRUCT: { |
| 744 |
// Struct pattern is always covered by a other struct constructor. |
| 745 |
return true; |
| 746 |
} |
| 747 |
break; |
| 748 |
// TODO: support references |
| 749 |
case ConstructorKind::REFERENCE: |
| 750 |
default: |
| 751 |
rust_unreachable (); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
bool |
| 756 |
Constructor::operator< (const Constructor &o) const |
| 757 |
{ |
| 758 |
if (kind != o.kind) |
| 759 |
return kind < o.kind; |
| 760 |
|
| 761 |
switch (kind) |
| 762 |
{ |
| 763 |
case ConstructorKind::VARIANT: |
| 764 |
return variant_idx < o.variant_idx; |
| 765 |
case ConstructorKind::INT_RANGE: |
| 766 |
return int_range.lo < o.int_range.lo |
| 767 |
|| (int_range.lo == o.int_range.lo |
| 768 |
&& int_range.hi < o.int_range.hi); |
| 769 |
case ConstructorKind::STRUCT: |
| 770 |
case ConstructorKind::WILDCARD: |
| 771 |
case ConstructorKind::REFERENCE: |
| 772 |
return false; |
| 773 |
default: |
| 774 |
rust_unreachable (); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
std::string |
| 779 |
Constructor::to_string () const |
| 780 |
{ |
| 781 |
switch (kind) |
| 782 |
{ |
| 783 |
case ConstructorKind::STRUCT: |
| 784 |
return "STRUCT"; |
| 785 |
case ConstructorKind::VARIANT: |
| 786 |
return "VARIANT(" + std::to_string (variant_idx) + ")"; |
| 787 |
case ConstructorKind::INT_RANGE: |
| 788 |
return "RANGE" + std::to_string (int_range.lo) + ".." |
| 789 |
+ std::to_string (int_range.hi); |
| 790 |
case ConstructorKind::WILDCARD: |
| 791 |
return "_"; |
| 792 |
case ConstructorKind::REFERENCE: |
| 793 |
return "REF"; |
| 794 |
default: |
| 795 |
rust_unreachable (); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
std::vector<DeconstructedPat> |
| 800 |
DeconstructedPat::specialize (const Constructor &other_ctor, |
| 801 |
int other_ctor_arity) const |
| 802 |
{ |
| 803 |
rust_assert (other_ctor.is_covered_by (ctor)); |
| 804 |
if (ctor.is_wildcard ()) |
| 805 |
return std::vector<DeconstructedPat> ( |
| 806 |
other_ctor_arity, |
| 807 |
DeconstructedPat (Constructor::make_wildcard (), locus)); |
| 808 |
|
| 809 |
return fields; |
| 810 |
} |
| 811 |
|
| 812 |
std::string |
| 813 |
DeconstructedPat::to_string () const |
| 814 |
{ |
| 815 |
std::string s = ctor.to_string () + "["; |
| 816 |
for (auto &f : fields) |
| 817 |
s += f.to_string () + ", "; |
| 818 |
|
| 819 |
s += "](arity=" + std::to_string (arity) + ")"; |
| 820 |
return s; |
| 821 |
} |
| 822 |
|
| 823 |
bool |
| 824 |
PatOrWild::is_covered_by (const Constructor &c) const |
| 825 |
{ |
| 826 |
if (pat.has_value ()) |
| 827 |
return pat.value ().get_ctor ().is_covered_by (c); |
| 828 |
else |
| 829 |
return true; |
| 830 |
} |
| 831 |
|
| 832 |
std::vector<PatOrWild> |
| 833 |
PatOrWild::specialize (const Constructor &other_ctor, |
| 834 |
int other_ctor_arity) const |
| 835 |
{ |
| 836 |
if (pat.has_value ()) |
| 837 |
{ |
| 838 |
auto v = pat.value ().specialize (other_ctor, other_ctor_arity); |
| 839 |
std::vector<PatOrWild> ret; |
| 840 |
for (auto &pat : v) |
| 841 |
ret.push_back (PatOrWild::make_pattern (pat)); |
| 842 |
|
| 843 |
return ret; |
| 844 |
} |
| 845 |
else |
| 846 |
{ |
| 847 |
return std::vector<PatOrWild> (other_ctor_arity, |
| 848 |
PatOrWild::make_wildcard ()); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
std::string |
| 853 |
PatOrWild::to_string () const |
| 854 |
{ |
| 855 |
if (pat.has_value ()) |
| 856 |
return pat.value ().to_string (); |
| 857 |
else |
| 858 |
return "Wild"; |
| 859 |
} |
| 860 |
|
| 861 |
void |
| 862 |
PatStack::pop_head_constructor (const Constructor &other_ctor, |
| 863 |
int other_ctor_arity) |
| 864 |
{ |
| 865 |
rust_assert (!pats.empty ()); |
| 866 |
rust_assert (other_ctor.is_covered_by (head ().ctor ())); |
| 867 |
|
| 868 |
PatOrWild &hd = head (); |
| 869 |
auto v = hd.specialize (other_ctor, other_ctor_arity); |
| 870 |
{ |
| 871 |
std::string s = "["; |
| 872 |
for (auto &pat : v) |
| 873 |
s += pat.to_string () + ", "; |
| 874 |
s += "]"; |
| 875 |
|
| 876 |
rust_debug ("specialize %s with %s to %s", hd.to_string ().c_str (), |
| 877 |
other_ctor.to_string ().c_str (), s.c_str ()); |
| 878 |
} |
| 879 |
pop_head (); |
| 880 |
for (auto &pat : v) |
| 881 |
pats.push_back (pat); |
| 882 |
} |
| 883 |
|
| 884 |
std::string |
| 885 |
MatrixRow::to_string () const |
| 886 |
{ |
| 887 |
std::string s; |
| 888 |
for (const PatOrWild &pat : pats.get_subpatterns ()) |
| 889 |
s += pat.to_string () + ", "; |
| 890 |
return s; |
| 891 |
} |
| 892 |
|
| 893 |
std::vector<PlaceInfo> |
| 894 |
PlaceInfo::specialize (const Constructor &c) const |
| 895 |
{ |
| 896 |
switch (c.get_kind ()) |
| 897 |
{ |
| 898 |
case Constructor::ConstructorKind::WILDCARD: |
| 899 |
case Constructor::ConstructorKind::INT_RANGE: { |
| 900 |
return {}; |
| 901 |
} |
| 902 |
break; |
| 903 |
case Constructor::ConstructorKind::STRUCT: |
| 904 |
case Constructor::ConstructorKind::VARIANT: { |
| 905 |
rust_assert (ty->get_kind () == TyTy::TypeKind::ADT); |
| 906 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (ty); |
| 907 |
switch (adt->get_adt_kind ()) |
| 908 |
{ |
| 909 |
case TyTy::ADTType::ADTKind::ENUM: |
| 910 |
case TyTy::ADTType::ADTKind::STRUCT_STRUCT: |
| 911 |
case TyTy::ADTType::ADTKind::TUPLE_STRUCT: { |
| 912 |
TyTy::VariantDef *variant |
| 913 |
= adt->get_variants ().at (c.get_variant_index ()); |
| 914 |
if (variant->get_variant_type () |
| 915 |
== TyTy::VariantDef::VariantType::NUM) |
| 916 |
return {}; |
| 917 |
|
| 918 |
std::vector<PlaceInfo> new_place_infos; |
| 919 |
for (auto &field : variant->get_fields ()) |
| 920 |
new_place_infos.push_back (field->get_field_type ()); |
| 921 |
|
| 922 |
return new_place_infos; |
| 923 |
} |
| 924 |
break; |
| 925 |
case TyTy::ADTType::ADTKind::UNION: { |
| 926 |
// TODO: support unions |
| 927 |
rust_unreachable (); |
| 928 |
} |
| 929 |
} |
| 930 |
} |
| 931 |
break; |
| 932 |
default: { |
| 933 |
rust_unreachable (); |
| 934 |
} |
| 935 |
break; |
| 936 |
} |
| 937 |
|
| 938 |
rust_unreachable (); |
| 939 |
} |
| 940 |
|
| 941 |
Matrix |
| 942 |
Matrix::specialize (const Constructor &ctor) const |
| 943 |
{ |
| 944 |
auto subfields_place_info = place_infos.at (0).specialize (ctor); |
| 945 |
|
| 946 |
std::vector<MatrixRow> new_rows; |
| 947 |
for (const MatrixRow &row : rows) |
| 948 |
{ |
| 949 |
PatStack pats = row.get_pats_clone (); |
| 950 |
const PatOrWild &hd = pats.head (); |
| 951 |
if (ctor.is_covered_by (hd.ctor ())) |
| 952 |
{ |
| 953 |
pats.pop_head_constructor (ctor, subfields_place_info.size ()); |
| 954 |
new_rows.push_back (MatrixRow (pats, row.is_under_guard ())); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
if (place_infos.empty ()) |
| 959 |
return Matrix (new_rows, {}); |
| 960 |
|
| 961 |
// push subfields of the first fields after specialization |
| 962 |
std::vector<PlaceInfo> new_place_infos = subfields_place_info; |
| 963 |
// add place infos for the rest of the fields |
| 964 |
for (size_t i = 1; i < place_infos.size (); i++) |
| 965 |
new_place_infos.push_back (place_infos.at (i)); |
| 966 |
|
| 967 |
return Matrix (new_rows, new_place_infos); |
| 968 |
} |
| 969 |
|
| 970 |
std::string |
| 971 |
Matrix::to_string () const |
| 972 |
{ |
| 973 |
std::string s = "[\n"; |
| 974 |
for (const MatrixRow &row : rows) |
| 975 |
s += "row: " + row.to_string () + "\n"; |
| 976 |
|
| 977 |
s += "](place_infos=["; |
| 978 |
for (const PlaceInfo &place_info : place_infos) |
| 979 |
s += place_info.get_type ()->as_string () + ", "; |
| 980 |
|
| 981 |
s += "])"; |
| 982 |
return s; |
| 983 |
} |
| 984 |
|
| 985 |
std::string |
| 986 |
WitnessPat::to_string () const |
| 987 |
{ |
| 988 |
switch (ctor.get_kind ()) |
| 989 |
{ |
| 990 |
case Constructor::ConstructorKind::STRUCT: { |
| 991 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (ty); |
| 992 |
TyTy::VariantDef *variant |
| 993 |
= adt->get_variants ().at (ctor.get_variant_index ()); |
| 994 |
std::string buf; |
| 995 |
buf += adt->get_identifier (); |
| 996 |
|
| 997 |
buf += " {"; |
| 998 |
if (!fields.empty ()) |
| 999 |
buf += " "; |
| 1000 |
|
| 1001 |
for (size_t i = 0; i < fields.size (); i++) |
| 1002 |
{ |
| 1003 |
buf += variant->get_fields ().at (i)->get_name () + ": "; |
| 1004 |
buf += fields.at (i).to_string (); |
| 1005 |
if (i < fields.size () - 1) |
| 1006 |
buf += ", "; |
| 1007 |
} |
| 1008 |
if (!fields.empty ()) |
| 1009 |
buf += " "; |
| 1010 |
|
| 1011 |
buf += "}"; |
| 1012 |
return buf; |
| 1013 |
} |
| 1014 |
break; |
| 1015 |
case Constructor::ConstructorKind::VARIANT: { |
| 1016 |
std::string buf; |
| 1017 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (ty); |
| 1018 |
buf += adt->get_identifier (); |
| 1019 |
TyTy::VariantDef *variant |
| 1020 |
= adt->get_variants ().at (ctor.get_variant_index ()); |
| 1021 |
buf += "::" + variant->get_identifier (); |
| 1022 |
|
| 1023 |
switch (variant->get_variant_type ()) |
| 1024 |
{ |
| 1025 |
case TyTy::VariantDef::VariantType::NUM: { |
| 1026 |
return buf; |
| 1027 |
} |
| 1028 |
break; |
| 1029 |
case TyTy::VariantDef::VariantType::TUPLE: { |
| 1030 |
buf += "("; |
| 1031 |
for (size_t i = 0; i < fields.size (); i++) |
| 1032 |
{ |
| 1033 |
buf += fields.at (i).to_string (); |
| 1034 |
if (i < fields.size () - 1) |
| 1035 |
buf += ", "; |
| 1036 |
} |
| 1037 |
buf += ")"; |
| 1038 |
return buf; |
| 1039 |
} |
| 1040 |
break; |
| 1041 |
case TyTy::VariantDef::VariantType::STRUCT: { |
| 1042 |
buf += " {"; |
| 1043 |
if (!fields.empty ()) |
| 1044 |
buf += " "; |
| 1045 |
|
| 1046 |
for (size_t i = 0; i < fields.size (); i++) |
| 1047 |
{ |
| 1048 |
buf += variant->get_fields ().at (i)->get_name () + ": "; |
| 1049 |
buf += fields.at (i).to_string (); |
| 1050 |
if (i < fields.size () - 1) |
| 1051 |
buf += ", "; |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (!fields.empty ()) |
| 1055 |
buf += " "; |
| 1056 |
|
| 1057 |
buf += "}"; |
| 1058 |
} |
| 1059 |
break; |
| 1060 |
default: { |
| 1061 |
rust_unreachable (); |
| 1062 |
} |
| 1063 |
break; |
| 1064 |
} |
| 1065 |
return buf; |
| 1066 |
} |
| 1067 |
break; |
| 1068 |
case Constructor::ConstructorKind::INT_RANGE: { |
| 1069 |
// TODO: implement |
| 1070 |
rust_unreachable (); |
| 1071 |
} |
| 1072 |
break; |
| 1073 |
case Constructor::ConstructorKind::WILDCARD: { |
| 1074 |
return "_"; |
| 1075 |
} |
| 1076 |
break; |
| 1077 |
case Constructor::ConstructorKind::REFERENCE: { |
| 1078 |
// TODO: implement |
| 1079 |
rust_unreachable (); |
| 1080 |
} |
| 1081 |
break; |
| 1082 |
default: { |
| 1083 |
rust_unreachable (); |
| 1084 |
} |
| 1085 |
break; |
| 1086 |
} |
| 1087 |
rust_unreachable (); |
| 1088 |
} |
| 1089 |
|
| 1090 |
void |
| 1091 |
WitnessMatrix::apply_constructor (const Constructor &ctor, |
| 1092 |
const std::set<Constructor> &missings, |
| 1093 |
TyTy::BaseType *ty) |
| 1094 |
{ |
| 1095 |
int arity = 0; |
| 1096 |
// TODO: only support struct and variant ctor for now. |
| 1097 |
switch (ctor.get_kind ()) |
| 1098 |
{ |
| 1099 |
case Constructor::ConstructorKind::WILDCARD: { |
| 1100 |
arity = 0; |
| 1101 |
} |
| 1102 |
break; |
| 1103 |
case Constructor::ConstructorKind::STRUCT: |
| 1104 |
case Constructor::ConstructorKind::VARIANT: { |
| 1105 |
if (ty->get_kind () == TyTy::TypeKind::ADT) |
| 1106 |
{ |
| 1107 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (ty); |
| 1108 |
TyTy::VariantDef *variant |
| 1109 |
= adt->get_variants ().at (ctor.get_variant_index ()); |
| 1110 |
if (variant->get_variant_type () == TyTy::VariantDef::NUM) |
| 1111 |
arity = 0; |
| 1112 |
else |
| 1113 |
arity = variant->get_fields ().size (); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
break; |
| 1117 |
default: { |
| 1118 |
rust_unreachable (); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
|
| 1122 |
std::string buf; |
| 1123 |
for (auto &stack : patstacks) |
| 1124 |
{ |
| 1125 |
buf += "["; |
| 1126 |
for (auto &pat : stack) |
| 1127 |
buf += pat.to_string () + ", "; |
| 1128 |
|
| 1129 |
buf += "]\n"; |
| 1130 |
} |
| 1131 |
rust_debug ("witness pats:\n%s", buf.c_str ()); |
| 1132 |
|
| 1133 |
for (auto &stack : patstacks) |
| 1134 |
{ |
| 1135 |
std::vector<WitnessPat> subfield; |
| 1136 |
for (int i = 0; i < arity; i++) |
| 1137 |
{ |
| 1138 |
if (stack.empty ()) |
| 1139 |
subfield.push_back (WitnessPat::make_wildcard (ty)); |
| 1140 |
else |
| 1141 |
{ |
| 1142 |
subfield.push_back (stack.back ()); |
| 1143 |
stack.pop_back (); |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|
| 1147 |
stack.push_back (WitnessPat (ctor, subfield, ty)); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
void |
| 1152 |
WitnessMatrix::extend (const WitnessMatrix &other) |
| 1153 |
{ |
| 1154 |
patstacks.insert (patstacks.end (), other.patstacks.begin (), |
| 1155 |
other.patstacks.end ()); |
| 1156 |
} |
| 1157 |
|
| 1158 |
// forward declarations |
| 1159 |
static DeconstructedPat |
| 1160 |
lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern &pattern, |
| 1161 |
TyTy::BaseType *scrutinee_ty); |
| 1162 |
|
| 1163 |
static DeconstructedPat |
| 1164 |
lower_tuple_pattern (Resolver::TypeCheckContext *ctx, |
| 1165 |
HIR::TupleStructPattern &pattern, |
| 1166 |
TyTy::VariantDef *variant, Constructor &ctor) |
| 1167 |
{ |
| 1168 |
int arity = variant->get_fields ().size (); |
| 1169 |
HIR::TupleStructItems &elems = pattern.get_items (); |
| 1170 |
|
| 1171 |
std::vector<DeconstructedPat> fields; |
| 1172 |
switch (elems.get_item_type ()) |
| 1173 |
{ |
| 1174 |
case HIR::TupleStructItems::ItemType::MULTIPLE: { |
| 1175 |
HIR::TupleStructItemsNoRange &multiple |
| 1176 |
= static_cast<HIR::TupleStructItemsNoRange &> (elems); |
| 1177 |
|
| 1178 |
rust_assert (variant->get_fields ().size () |
| 1179 |
== multiple.get_patterns ().size ()); |
| 1180 |
|
| 1181 |
for (size_t i = 0; i < multiple.get_patterns ().size (); i++) |
| 1182 |
{ |
| 1183 |
fields.push_back ( |
| 1184 |
lower_pattern (ctx, *multiple.get_patterns ().at (i), |
| 1185 |
variant->get_fields ().at (i)->get_field_type ())); |
| 1186 |
} |
| 1187 |
return DeconstructedPat (ctor, arity, fields, pattern.get_locus ()); |
| 1188 |
} |
| 1189 |
break; |
| 1190 |
case HIR::TupleStructItems::ItemType::RANGED: { |
| 1191 |
// TODO: ranged tuple struct items |
| 1192 |
rust_unreachable (); |
| 1193 |
} |
| 1194 |
break; |
| 1195 |
default: { |
| 1196 |
rust_unreachable (); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
static DeconstructedPat |
| 1202 |
lower_struct_pattern (Resolver::TypeCheckContext *ctx, |
| 1203 |
HIR::StructPattern &pattern, TyTy::VariantDef *variant, |
| 1204 |
Constructor ctor) |
| 1205 |
{ |
| 1206 |
int arity = variant->get_fields ().size (); |
| 1207 |
|
| 1208 |
// Initialize all field patterns to wildcard. |
| 1209 |
std::vector<DeconstructedPat> fields |
| 1210 |
= std::vector<DeconstructedPat> (arity, DeconstructedPat::make_wildcard ( |
| 1211 |
pattern.get_locus ())); |
| 1212 |
|
| 1213 |
std::map<std::string, int> field_map; |
| 1214 |
for (int i = 0; i < arity; i++) |
| 1215 |
{ |
| 1216 |
auto &f = variant->get_fields ().at (i); |
| 1217 |
field_map[f->get_name ()] = i; |
| 1218 |
} |
| 1219 |
|
| 1220 |
// Fill in the fields with the present patterns. |
| 1221 |
HIR::StructPatternElements elems = pattern.get_struct_pattern_elems (); |
| 1222 |
for (auto &elem : elems.get_struct_pattern_fields ()) |
| 1223 |
{ |
| 1224 |
switch (elem->get_item_type ()) |
| 1225 |
{ |
| 1226 |
case HIR::StructPatternField::ItemType::IDENT: { |
| 1227 |
HIR::StructPatternFieldIdent *ident |
| 1228 |
= static_cast<HIR::StructPatternFieldIdent *> (elem.get ()); |
| 1229 |
int field_idx |
| 1230 |
= field_map.at (ident->get_identifier ().as_string ()); |
| 1231 |
fields.at (field_idx) |
| 1232 |
= DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1233 |
} |
| 1234 |
break; |
| 1235 |
case HIR::StructPatternField::ItemType::IDENT_PAT: { |
| 1236 |
HIR::StructPatternFieldIdentPat *ident_pat |
| 1237 |
= static_cast<HIR::StructPatternFieldIdentPat *> (elem.get ()); |
| 1238 |
int field_idx |
| 1239 |
= field_map.at (ident_pat->get_identifier ().as_string ()); |
| 1240 |
fields.at (field_idx) = lower_pattern ( |
| 1241 |
ctx, ident_pat->get_pattern (), |
| 1242 |
variant->get_fields ().at (field_idx)->get_field_type ()); |
| 1243 |
} |
| 1244 |
break; |
| 1245 |
case HIR::StructPatternField::ItemType::TUPLE_PAT: { |
| 1246 |
// TODO: tuple: pat |
| 1247 |
rust_unreachable (); |
| 1248 |
} |
| 1249 |
break; |
| 1250 |
default: { |
| 1251 |
rust_unreachable (); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
return DeconstructedPat{ctor, arity, fields, pattern.get_locus ()}; |
| 1257 |
}; |
| 1258 |
|
| 1259 |
static DeconstructedPat |
| 1260 |
lower_pattern (Resolver::TypeCheckContext *ctx, HIR::Pattern &pattern, |
| 1261 |
TyTy::BaseType *scrutinee_ty) |
| 1262 |
{ |
| 1263 |
HIR::Pattern::PatternType pat_type = pattern.get_pattern_type (); |
| 1264 |
switch (pat_type) |
| 1265 |
{ |
| 1266 |
case HIR::Pattern::PatternType::WILDCARD: |
| 1267 |
case HIR::Pattern::PatternType::IDENTIFIER: { |
| 1268 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1269 |
} |
| 1270 |
break; |
| 1271 |
case HIR::Pattern::PatternType::PATH: { |
| 1272 |
// TODO: support constants, associated constants, enum variants and |
| 1273 |
// structs |
| 1274 |
// https://doc.rust-lang.org/reference/patterns.html#path-patterns |
| 1275 |
// unimplemented. Treat this pattern as wildcard for now. |
| 1276 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1277 |
} |
| 1278 |
break; |
| 1279 |
case HIR::Pattern::PatternType::REFERENCE: { |
| 1280 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1281 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1282 |
} |
| 1283 |
break; |
| 1284 |
case HIR::Pattern::PatternType::STRUCT: |
| 1285 |
case HIR::Pattern::PatternType::TUPLE_STRUCT: { |
| 1286 |
HirId path_id = UNKNOWN_HIRID; |
| 1287 |
if (pat_type == HIR::Pattern::PatternType::STRUCT) |
| 1288 |
{ |
| 1289 |
HIR::StructPattern &struct_pattern |
| 1290 |
= static_cast<HIR::StructPattern &> (pattern); |
| 1291 |
path_id = struct_pattern.get_path ().get_mappings ().get_hirid (); |
| 1292 |
} |
| 1293 |
else |
| 1294 |
{ |
| 1295 |
HIR::TupleStructPattern &tuple_pattern |
| 1296 |
= static_cast<HIR::TupleStructPattern &> (pattern); |
| 1297 |
path_id = tuple_pattern.get_path ().get_mappings ().get_hirid (); |
| 1298 |
} |
| 1299 |
|
| 1300 |
rust_assert (scrutinee_ty->get_kind () == TyTy::TypeKind::ADT); |
| 1301 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (scrutinee_ty); |
| 1302 |
|
| 1303 |
Constructor ctor = Constructor::make_struct (); |
| 1304 |
TyTy::VariantDef *variant; |
| 1305 |
if (adt->is_struct_struct () || adt->is_tuple_struct ()) |
| 1306 |
variant = adt->get_variants ().at (0); |
| 1307 |
else if (adt->is_enum ()) |
| 1308 |
{ |
| 1309 |
HirId variant_id = UNKNOWN_HIRID; |
| 1310 |
bool ok = ctx->lookup_variant_definition (path_id, &variant_id); |
| 1311 |
rust_assert (ok); |
| 1312 |
|
| 1313 |
int variant_idx; |
| 1314 |
ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_idx); |
| 1315 |
rust_assert (ok); |
| 1316 |
|
| 1317 |
ctor = Constructor::make_variant (variant_idx); |
| 1318 |
} |
| 1319 |
else |
| 1320 |
{ |
| 1321 |
rust_unreachable (); |
| 1322 |
} |
| 1323 |
rust_assert (variant->get_variant_type () |
| 1324 |
== TyTy::VariantDef::VariantType::TUPLE |
| 1325 |
|| variant->get_variant_type () |
| 1326 |
== TyTy::VariantDef::VariantType::STRUCT); |
| 1327 |
|
| 1328 |
if (pat_type == HIR::Pattern::PatternType::STRUCT) |
| 1329 |
{ |
| 1330 |
HIR::StructPattern &struct_pattern |
| 1331 |
= static_cast<HIR::StructPattern &> (pattern); |
| 1332 |
return lower_struct_pattern (ctx, struct_pattern, variant, ctor); |
| 1333 |
} |
| 1334 |
else |
| 1335 |
{ |
| 1336 |
HIR::TupleStructPattern &tuple_pattern |
| 1337 |
= static_cast<HIR::TupleStructPattern &> (pattern); |
| 1338 |
return lower_tuple_pattern (ctx, tuple_pattern, variant, ctor); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
break; |
| 1342 |
case HIR::Pattern::PatternType::TUPLE: { |
| 1343 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1344 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1345 |
} |
| 1346 |
break; |
| 1347 |
case HIR::Pattern::PatternType::SLICE: { |
| 1348 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1349 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1350 |
} |
| 1351 |
break; |
| 1352 |
case HIR::Pattern::PatternType::ALT: { |
| 1353 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1354 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1355 |
} |
| 1356 |
break; |
| 1357 |
case HIR::Pattern::PatternType::LITERAL: { |
| 1358 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1359 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1360 |
} |
| 1361 |
break; |
| 1362 |
case HIR::Pattern::PatternType::RANGE: { |
| 1363 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1364 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1365 |
} |
| 1366 |
break; |
| 1367 |
case HIR::Pattern::PatternType::GROUPED: { |
| 1368 |
// TODO: unimplemented. Treat this pattern as wildcard for now. |
| 1369 |
return DeconstructedPat::make_wildcard (pattern.get_locus ()); |
| 1370 |
} |
| 1371 |
break; |
| 1372 |
default: { |
| 1373 |
rust_unreachable (); |
| 1374 |
} |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
static MatchArm |
| 1379 |
lower_arm (Resolver::TypeCheckContext *ctx, HIR::MatchCase &arm, |
| 1380 |
TyTy::BaseType *scrutinee_ty) |
| 1381 |
{ |
| 1382 |
rust_assert (arm.get_arm ().get_patterns ().size () > 0); |
| 1383 |
|
| 1384 |
DeconstructedPat pat |
| 1385 |
= lower_pattern (ctx, *arm.get_arm ().get_patterns ().at (0), scrutinee_ty); |
| 1386 |
return MatchArm (pat, arm.get_arm ().has_match_arm_guard ()); |
| 1387 |
} |
| 1388 |
|
| 1389 |
std::pair<std::set<Constructor>, std::set<Constructor>> |
| 1390 |
split_constructors (std::vector<Constructor> &ctors, PlaceInfo &place_info) |
| 1391 |
{ |
| 1392 |
bool all_wildcard = true; |
| 1393 |
for (auto &ctor : ctors) |
| 1394 |
{ |
| 1395 |
if (!ctor.is_wildcard ()) |
| 1396 |
all_wildcard = false; |
| 1397 |
} |
| 1398 |
|
| 1399 |
// first pass for the case that all patterns are wildcard |
| 1400 |
if (all_wildcard) |
| 1401 |
return std::make_pair (std::set<Constructor> ( |
| 1402 |
{Constructor::make_wildcard ()}), |
| 1403 |
std::set<Constructor> ()); |
| 1404 |
|
| 1405 |
// TODO: only support enums and structs for now. |
| 1406 |
TyTy::BaseType *ty = place_info.get_type (); |
| 1407 |
rust_assert (ty->get_kind () == TyTy::TypeKind::ADT); |
| 1408 |
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (ty); |
| 1409 |
rust_assert (adt->is_enum () || adt->is_struct_struct () |
| 1410 |
|| adt->is_tuple_struct ()); |
| 1411 |
|
| 1412 |
std::set<Constructor> universe; |
| 1413 |
if (adt->is_enum ()) |
| 1414 |
{ |
| 1415 |
for (size_t i = 0; i < adt->get_variants ().size (); i++) |
| 1416 |
universe.insert (Constructor::make_variant (i)); |
| 1417 |
} |
| 1418 |
else if (adt->is_struct_struct () || adt->is_tuple_struct ()) |
| 1419 |
{ |
| 1420 |
universe.insert (Constructor::make_struct ()); |
| 1421 |
} |
| 1422 |
|
| 1423 |
std::set<Constructor> present; |
| 1424 |
for (auto &ctor : ctors) |
| 1425 |
{ |
| 1426 |
if (ctor.is_wildcard ()) |
| 1427 |
return std::make_pair (universe, std::set<Constructor> ()); |
| 1428 |
else |
| 1429 |
present.insert (ctor); |
| 1430 |
} |
| 1431 |
|
| 1432 |
std::set<Constructor> missing; |
| 1433 |
std::set_difference (universe.begin (), universe.end (), present.begin (), |
| 1434 |
present.end (), std::inserter (missing, missing.end ())); |
| 1435 |
return std::make_pair (universe, missing); |
| 1436 |
} |
| 1437 |
|
| 1438 |
// The core of the algorithm. It computes the usefulness and exhaustiveness of a |
| 1439 |
// given matrix recursively. |
| 1440 |
// TODO: calculate usefulness |
| 1441 |
static WitnessMatrix |
| 1442 |
compute_exhaustiveness_and_usefulness (Resolver::TypeCheckContext *ctx, |
| 1443 |
Matrix &matrix) |
| 1444 |
{ |
| 1445 |
rust_debug ("call compute_exhaustiveness_and_usefulness"); |
| 1446 |
rust_debug ("matrix: %s", matrix.to_string ().c_str ()); |
| 1447 |
|
| 1448 |
if (matrix.get_rows ().empty ()) |
| 1449 |
{ |
| 1450 |
// no rows left. This means a non-exhaustive pattern. |
| 1451 |
rust_debug ("non-exhaustive subpattern found"); |
| 1452 |
return WitnessMatrix::make_unit (); |
| 1453 |
} |
| 1454 |
|
| 1455 |
// Base case: there are no columns in matrix. |
| 1456 |
if (matrix.get_place_infos ().empty ()) |
| 1457 |
return WitnessMatrix::make_empty (); |
| 1458 |
|
| 1459 |
std::vector<Constructor> heads; |
| 1460 |
for (auto head : matrix.heads ()) |
| 1461 |
heads.push_back (head.ctor ()); |
| 1462 |
|
| 1463 |
// TODO: not sure missing ctors need to be calculated |
| 1464 |
auto ctors_and_missings |
| 1465 |
= split_constructors (heads, matrix.get_place_infos ().at (0)); |
| 1466 |
std::set<Constructor> ctors = ctors_and_missings.first; |
| 1467 |
std::set<Constructor> missings = ctors_and_missings.second; |
| 1468 |
|
| 1469 |
WitnessMatrix ret = WitnessMatrix::make_empty (); |
| 1470 |
for (auto &ctor : ctors) |
| 1471 |
{ |
| 1472 |
rust_debug ("specialize with %s", ctor.to_string ().c_str ()); |
| 1473 |
// TODO: Instead of creating new matrix, we can change the original matrix |
| 1474 |
// and use it for sub-pattern matching. It will significantly reduce |
| 1475 |
// memory usage. |
| 1476 |
Matrix spec_matrix = matrix.specialize (ctor); |
| 1477 |
|
| 1478 |
WitnessMatrix witness |
| 1479 |
= compute_exhaustiveness_and_usefulness (ctx, spec_matrix); |
| 1480 |
|
| 1481 |
TyTy::BaseType *ty = matrix.get_place_infos ().at (0).get_type (); |
| 1482 |
witness.apply_constructor (ctor, missings, ty); |
| 1483 |
ret.extend (witness); |
| 1484 |
} |
| 1485 |
|
| 1486 |
return ret; |
| 1487 |
} |
| 1488 |
|
| 1489 |
static void |
| 1490 |
emit_exhaustiveness_error (Resolver::TypeCheckContext *ctx, |
| 1491 |
HIR::MatchExpr &expr, WitnessMatrix &witness) |
| 1492 |
{ |
| 1493 |
TyTy::BaseType *scrutinee_ty; |
| 1494 |
bool ok |
| 1495 |
= ctx->lookup_type (expr.get_scrutinee_expr ().get_mappings ().get_hirid (), |
| 1496 |
&scrutinee_ty); |
| 1497 |
rust_assert (ok); |
| 1498 |
|
| 1499 |
if (!witness.empty ()) |
| 1500 |
{ |
| 1501 |
std::stringstream buf; |
| 1502 |
for (size_t i = 0; i < witness.get_stacks ().size (); i++) |
| 1503 |
{ |
| 1504 |
auto &stack = witness.get_stacks ().at (i); |
| 1505 |
WitnessPat w = WitnessPat::make_wildcard (scrutinee_ty); |
| 1506 |
if (!stack.empty ()) |
| 1507 |
w = stack.at (0); |
| 1508 |
|
| 1509 |
rust_debug ("Witness[%d]: %s", (int) i, w.to_string ().c_str ()); |
| 1510 |
buf << "'" << w.to_string () << "'"; |
| 1511 |
if (i != witness.get_stacks ().size () - 1) |
| 1512 |
buf << " and "; |
| 1513 |
} |
| 1514 |
rust_error_at (expr.get_scrutinee_expr ().get_locus (), |
| 1515 |
"non-exhaustive patterns: %s not covered", |
| 1516 |
buf.str ().c_str ()); |
| 1517 |
} |
| 1518 |
else |
| 1519 |
{ |
| 1520 |
rust_debug ("no witness found"); |
| 1521 |
} |
| 1522 |
} |
| 1523 |
|
| 1524 |
// Entry point for computing match usefulness and check exhaustiveness |
| 1525 |
void |
| 1526 |
check_match_usefulness (Resolver::TypeCheckContext *ctx, |
| 1527 |
TyTy::BaseType *scrutinee_ty, HIR::MatchExpr &expr) |
| 1528 |
{ |
| 1529 |
if (!expr.has_match_arms ()) |
| 1530 |
return; |
| 1531 |
|
| 1532 |
// Lower the arms to a more convenient representation. |
| 1533 |
std::vector<MatrixRow> rows; |
| 1534 |
for (auto &arm : expr.get_match_cases ()) |
| 1535 |
{ |
| 1536 |
PatStack pats; |
| 1537 |
MatchArm lowered = lower_arm (ctx, arm, scrutinee_ty); |
| 1538 |
PatOrWild pat = PatOrWild::make_pattern (lowered.get_pat ()); |
| 1539 |
pats.push (pat); |
| 1540 |
rows.push_back (MatrixRow (pats, lowered.has_guard ())); |
| 1541 |
} |
| 1542 |
|
| 1543 |
std::vector<PlaceInfo> place_infos = {{PlaceInfo (scrutinee_ty)}}; |
| 1544 |
Matrix matrix{rows, place_infos}; |
| 1545 |
|
| 1546 |
WitnessMatrix witness = compute_exhaustiveness_and_usefulness (ctx, matrix); |
| 1547 |
|
| 1548 |
emit_exhaustiveness_error (ctx, expr, witness); |
| 1549 |
} |
| 1550 |
|
| 1551 |
} // namespace Analysis |
| 1552 |
} // namespace Rust |