|
30 | 30 | #include "rust-hir-item.h" |
31 | 31 | #include "rust-session-manager.h" |
32 | 32 | #include "rust-immutable-name-resolution-context.h" |
| 33 | +#include "rust-name-resolver.h" |
| 34 | +#include "rust-hir-expr.h" |
33 | 35 |
|
34 | 36 | namespace Rust { |
35 | 37 | namespace Resolver { |
@@ -241,6 +243,66 @@ TypeCheckExpr::visit (HIR::PathInExpression &expr) |
241 | 243 | } |
242 | 244 | } |
243 | 245 |
|
| 246 | +/* Helper to check if a const generic expression is dependent (symbolic). |
| 247 | + Returns true if the expression contains paths or identifiers (e.g., { N + 1 |
| 248 | + }). Returns false if the expression is purely literal/concrete (e.g., { 1 + 1 |
| 249 | + }). */ |
| 250 | +static bool |
| 251 | +is_const_dependent (HIR::Expr &expr) |
| 252 | +{ |
| 253 | + switch (expr.get_expression_type ()) |
| 254 | + { |
| 255 | + case HIR::Expr::ExprType::Path: |
| 256 | + { |
| 257 | + // A path is only dependent if it resolves to a generic parameter. |
| 258 | + // We use Resolver2_0 to find the definition ID. |
| 259 | + auto &nr_ctx |
| 260 | + = Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 261 | + auto resolved = nr_ctx.lookup (expr.get_mappings ().get_nodeid ()); |
| 262 | + |
| 263 | + if (!resolved) |
| 264 | + return false; |
| 265 | + |
| 266 | + return Analysis::Mappings::get ().lookup_hir_generic_param (*resolved) |
| 267 | + != nullptr; |
| 268 | + } |
| 269 | + |
| 270 | + case HIR::Expr::ExprType::Lit: |
| 271 | + return false; |
| 272 | + |
| 273 | + case HIR::Expr::ExprType::Block: |
| 274 | + { |
| 275 | + auto &block = static_cast<HIR::BlockExpr &> (expr); |
| 276 | + if (block.has_expr ()) |
| 277 | + |
| 278 | + return is_const_dependent (block.get_final_expr ()); |
| 279 | + |
| 280 | + if (!block.get_statements ().empty ()) |
| 281 | + return true; |
| 282 | + |
| 283 | + return false; |
| 284 | + } |
| 285 | + |
| 286 | + case HIR::Expr::ExprType::Grouped: |
| 287 | + { |
| 288 | + auto &group = static_cast<HIR::GroupedExpr &> (expr); |
| 289 | + |
| 290 | + return is_const_dependent (group.get_expr_in_parens ()); |
| 291 | + } |
| 292 | + |
| 293 | + case HIR::Expr::ExprType::Operator: |
| 294 | + { |
| 295 | + auto &arith = static_cast<HIR::ArithmeticOrLogicalExpr &> (expr); |
| 296 | + |
| 297 | + return is_const_dependent (arith.get_lhs ()) |
| 298 | + || is_const_dependent (arith.get_rhs ()); |
| 299 | + } |
| 300 | + |
| 301 | + default: |
| 302 | + return true; |
| 303 | + } |
| 304 | +} |
| 305 | + |
244 | 306 | TyTy::BaseType * |
245 | 307 | TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset, |
246 | 308 | NodeId *root_resolved_node_id) |
@@ -366,6 +428,25 @@ TypeCheckExpr::resolve_root_path (HIR::PathInExpression &expr, size_t *offset, |
366 | 428 | // turbo-fish segment path::<ty> |
367 | 429 | if (seg.has_generic_args ()) |
368 | 430 | { |
| 431 | + // Check for dependent const expressions (like { N + 1 }) |
| 432 | + bool is_dependent = false; |
| 433 | + for (auto &arg : seg.get_generic_args ().get_const_args ()) |
| 434 | + { |
| 435 | + if (is_const_dependent (*arg.get_expression ())) |
| 436 | + { |
| 437 | + is_dependent = true; |
| 438 | + break; |
| 439 | + } |
| 440 | + } |
| 441 | + |
| 442 | + if (is_dependent) |
| 443 | + { |
| 444 | + *root_resolved_node_id = ref_node_id; |
| 445 | + *offset = *offset + 1; |
| 446 | + root_tyty = lookup; |
| 447 | + continue; |
| 448 | + } |
| 449 | + |
369 | 450 | lookup = SubstMapper::Resolve (lookup, expr.get_locus (), |
370 | 451 | &seg.get_generic_args (), |
371 | 452 | context->regions_from_generic_args ( |
@@ -524,6 +605,21 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id, |
524 | 605 |
|
525 | 606 | if (seg.has_generic_args ()) |
526 | 607 | { |
| 608 | + // Check for dependent const expressions (like { N + 1 }) |
| 609 | + bool is_dependent = false; |
| 610 | + for (auto &arg : seg.get_generic_args ().get_const_args ()) |
| 611 | + { |
| 612 | + if (is_const_dependent (*arg.get_expression ())) |
| 613 | + { |
| 614 | + is_dependent = true; |
| 615 | + break; |
| 616 | + } |
| 617 | + } |
| 618 | + if (is_dependent) |
| 619 | + { |
| 620 | + continue; |
| 621 | + } |
| 622 | + |
527 | 623 | rust_debug_loc (seg.get_locus (), "applying segment generics: %s", |
528 | 624 | tyseg->as_string ().c_str ()); |
529 | 625 | tyseg |
|
0 commit comments