Skip to content

Commit de1ed2e

Browse files
bors[bot]philberty
andauthored
Merge #838
838: Add mssing mappings to HIR::Pattern r=philberty a=philberty These mappings are missing within the HIR but are required to complete typechecking of all patterns in match arms. As the fields of structures must bind their associated field's types to new names declared as part of the pattern, these mappings give access to the associated name-resolved NodeId's to figure this out. Co-authored-by: Philip Herron <[email protected]>
2 parents 8507a68 + 4d70990 commit de1ed2e

9 files changed

+325
-77
lines changed

gcc/rust/hir/rust-ast-lower-pattern.cc

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ ASTLoweringPattern::visit (AST::TupleStructPattern &pattern)
6161
break;
6262
}
6363

64+
auto crate_num = mappings->get_current_crate ();
65+
Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (),
66+
mappings->get_next_hir_id (crate_num),
67+
UNKNOWN_LOCAL_DEFID);
68+
6469
translated = new HIR::TupleStructPattern (
65-
*path, std::unique_ptr<HIR::TupleStructItems> (lowered));
70+
mapping, *path, std::unique_ptr<HIR::TupleStructItems> (lowered));
6671
}
6772

6873
void
@@ -96,19 +101,38 @@ ASTLoweringPattern::visit (AST::StructPattern &pattern)
96101
AST::StructPatternFieldIdent &ident
97102
= static_cast<AST::StructPatternFieldIdent &> (*field.get ());
98103

104+
auto crate_num = mappings->get_current_crate ();
105+
Analysis::NodeMapping mapping (crate_num, ident.get_node_id (),
106+
mappings->get_next_hir_id (
107+
crate_num),
108+
UNKNOWN_LOCAL_DEFID);
109+
99110
f = new HIR::StructPatternFieldIdent (
100-
ident.get_identifier (), ident.is_ref (),
111+
mapping, ident.get_identifier (), ident.is_ref (),
101112
ident.is_mut () ? Mutability::Mut : Mutability::Imm,
102113
ident.get_outer_attrs (), ident.get_locus ());
103114
}
104115
break;
105116
}
106117

118+
// insert the reverse mappings and locations
119+
auto crate_num = f->get_mappings ().get_crate_num ();
120+
auto field_id = f->get_mappings ().get_hirid ();
121+
auto field_node_id = f->get_mappings ().get_nodeid ();
122+
mappings->insert_location (crate_num, field_id, f->get_locus ());
123+
mappings->insert_node_to_hir (crate_num, field_node_id, field_id);
124+
125+
// add it to the lowered fields list
107126
fields.push_back (std::unique_ptr<HIR::StructPatternField> (f));
108127
}
109128

129+
auto crate_num = mappings->get_current_crate ();
130+
Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (),
131+
mappings->get_next_hir_id (crate_num),
132+
UNKNOWN_LOCAL_DEFID);
133+
110134
HIR::StructPatternElements elems (std::move (fields));
111-
translated = new HIR::StructPattern (*path, std::move (elems));
135+
translated = new HIR::StructPattern (mapping, *path, std::move (elems));
112136
}
113137

114138
} // namespace HIR

gcc/rust/hir/rust-ast-lower-pattern.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,32 @@ class ASTLoweringPattern : public ASTLoweringBase
3333
{
3434
ASTLoweringPattern resolver;
3535
pattern->accept_vis (resolver);
36+
3637
rust_assert (resolver.translated != nullptr);
38+
39+
resolver.mappings->insert_hir_pattern (
40+
resolver.translated->get_pattern_mappings ().get_crate_num (),
41+
resolver.translated->get_pattern_mappings ().get_hirid (),
42+
resolver.translated);
43+
resolver.mappings->insert_location (
44+
resolver.translated->get_pattern_mappings ().get_crate_num (),
45+
resolver.translated->get_pattern_mappings ().get_hirid (),
46+
pattern->get_locus ());
47+
3748
return resolver.translated;
3849
}
3950

4051
void visit (AST::IdentifierPattern &pattern) override
4152
{
53+
auto crate_num = mappings->get_current_crate ();
54+
Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (),
55+
mappings->get_next_hir_id (crate_num),
56+
UNKNOWN_LOCAL_DEFID);
57+
4258
std::unique_ptr<Pattern> to_bind;
4359
translated
44-
= new HIR::IdentifierPattern (pattern.get_ident (), pattern.get_locus (),
45-
pattern.get_is_ref (),
60+
= new HIR::IdentifierPattern (mapping, pattern.get_ident (),
61+
pattern.get_locus (), pattern.get_is_ref (),
4662
pattern.get_is_mut () ? Mutability::Mut
4763
: Mutability::Imm,
4864
std::move (to_bind));

gcc/rust/hir/tree/rust-hir-path.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ class PathInExpression : public PathPattern, public PathExpr
330330
== 0;
331331
}
332332

333+
Analysis::NodeMapping get_pattern_mappings () const override final
334+
{
335+
return get_mappings ();
336+
}
337+
333338
protected:
334339
/* Use covariance to implement clone function as returning this object rather
335340
* than base */
@@ -824,6 +829,11 @@ class QualifiedPathInExpression : public PathPattern, public PathExpr
824829

825830
Location get_locus () { return locus; }
826831

832+
Analysis::NodeMapping get_pattern_mappings () const override final
833+
{
834+
return get_mappings ();
835+
}
836+
827837
protected:
828838
/* Use covariance to implement clone function as returning this object rather
829839
* than base */

0 commit comments

Comments
 (0)