diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-01-07 11:57:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-07 11:57:24 +0000 |
commit | 230b55bb532338fec1a9217f9ff8808cc0043772 (patch) | |
tree | e8bf9fee6760bc117b69603a59f6a3bb2c1949ab /gcc | |
parent | bc27d113167ec6c6cc96b01c8559fc9b9219417c (diff) | |
parent | 34710b497e44d96b661ee89d50776313ef91b6b2 (diff) | |
download | gcc-230b55bb532338fec1a9217f9ff8808cc0043772.zip gcc-230b55bb532338fec1a9217f9ff8808cc0043772.tar.gz gcc-230b55bb532338fec1a9217f9ff8808cc0043772.tar.bz2 |
Merge #868
868: Add support for Wildcard pattern binding r=philberty a=philberty
Wildcard bindings allow us to bind expression to be unused such as:
let _ = 123;
They are more commonly used in destructuring of tuples such as:
let my_tuple = (1,2);
let (a,_) = my_tuple;
This is the initial basic support for the basic form of let _ = ...; and
it also allows us to ignore parameters within functions as well.
Fixes #557
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-fnparam.h | 10 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-var-decl.h | 9 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 5 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-pattern.h | 103 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.h | 17 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-pattern.h | 11 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/issue-557.rs | 4 |
7 files changed, 115 insertions, 44 deletions
diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h index 92a7357..a7bf52a 100644 --- a/gcc/rust/backend/rust-compile-fnparam.h +++ b/gcc/rust/backend/rust-compile-fnparam.h @@ -52,6 +52,16 @@ public: fndecl, pattern.get_identifier (), decl_type, address_taken, locus); } + void visit (HIR::WildcardPattern &pattern) override + { + decl_type = ctx->get_backend ()->immutable_type (decl_type); + + bool address_taken = false; + compiled_param + = ctx->get_backend ()->parameter_variable (fndecl, "_", decl_type, + address_taken, locus); + } + private: CompileFnParam (Context *ctx, tree fndecl, tree decl_type, Location locus, const HIR::FunctionParam ¶m) diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index a5c736d..4b52dcd 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -64,6 +64,15 @@ public: address_taken, locus); } + void visit (HIR::WildcardPattern &pattern) override + { + translated_type = ctx->get_backend ()->immutable_type (translated_type); + compiled_variable + = ctx->get_backend ()->local_variable (fndecl, "_", translated_type, + NULL /*decl_var*/, address_taken, + locus); + } + private: CompileVarDecl (Context *ctx, tree fndecl) : HIRCompileBase (ctx), fndecl (fndecl), diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 5bf1be5..c62ba3f 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -269,6 +269,11 @@ public: PathExprSegment &get_root_seg () { return segments.at (0); } PathExprSegment get_final_segment () const { return segments.back (); } + + PatternType get_pattern_type () const override final + { + return PatternType::PATH; + } }; /* HIR node representing a path-in-expression pattern (path that allows generic diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h index b0b123f..8dca54d 100644 --- a/gcc/rust/hir/tree/rust-hir-pattern.h +++ b/gcc/rust/hir/tree/rust-hir-pattern.h @@ -49,7 +49,7 @@ public: has_minus (has_minus), locus (locus), mappings (mappings) {} - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } void accept_vis (HIRFullVisitor &vis) override; @@ -58,6 +58,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::LITERAL; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -122,7 +127,7 @@ public: IdentifierPattern (IdentifierPattern &&other) = default; IdentifierPattern &operator= (IdentifierPattern &&other) = default; - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } bool is_mut () const { return mut == Mutability::Mut; } @@ -135,6 +140,11 @@ public: Identifier get_identifier () const { return variable_ident; } + PatternType get_pattern_type () const override final + { + return PatternType::IDENTIFIER; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -157,7 +167,7 @@ public: : locus (locus), mappings (mappings) {} - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } void accept_vis (HIRFullVisitor &vis) override; @@ -166,6 +176,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::WILDCARD; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -335,7 +350,7 @@ public: RangePattern (RangePattern &&other) = default; RangePattern &operator= (RangePattern &&other) = default; - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } void accept_vis (HIRFullVisitor &vis) override; @@ -344,6 +359,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::RANGE; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -406,6 +426,11 @@ public: Location get_locus () const override final { return locus; } + PatternType get_pattern_type () const override final + { + return PatternType::REFERENCE; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -671,7 +696,7 @@ public: bool has_struct_pattern_elems () const { return !elems.is_empty (); } - Location get_locus () const { return path.get_locus (); } + Location get_locus () const override { return path.get_locus (); } void accept_vis (HIRFullVisitor &vis) override; @@ -683,6 +708,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::STRUCT; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -894,7 +924,7 @@ public: TupleStructPattern (TupleStructPattern &&other) = default; TupleStructPattern &operator= (TupleStructPattern &&other) = default; - Location get_locus () const { return path.get_locus (); } + Location get_locus () const override { return path.get_locus (); } void accept_vis (HIRFullVisitor &vis) override; @@ -907,6 +937,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::TUPLE_STRUCT; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -940,41 +975,6 @@ protected: virtual TuplePatternItems *clone_tuple_pattern_items_impl () const = 0; }; -// Class representing TuplePattern patterns where there is only a single pattern -/*class TuplePatternItemsSingle : public TuplePatternItems { - // Pattern pattern; - std::unique_ptr<Pattern> pattern; - - public: - TuplePatternItemsSingle(Pattern* pattern) : pattern(pattern) {} - - // Copy constructor uses clone - TuplePatternItemsSingle(TuplePatternItemsSingle const& other) : - pattern(other.pattern->clone_pattern()) {} - - // Destructor - define here if required - - // Overload assignment operator to clone - TuplePatternItemsSingle& operator=(TuplePatternItemsSingle const& other) { - pattern = other.pattern->clone_pattern(); - - return *this; - } - - // move constructors - TuplePatternItemsSingle(TuplePatternItemsSingle&& other) = default; - TuplePatternItemsSingle& operator=(TuplePatternItemsSingle&& other) = -default; - - protected: - // Use covariance to implement clone function as returning this object -rather than base virtual TuplePatternItemsSingle* -clone_tuple_pattern_items_impl() const override { return new -TuplePatternItemsSingle(*this); - } -};*/ -// removed in favour of single-element TuplePatternItemsMultiple - // Class representing TuplePattern patterns where there are multiple patterns class TuplePatternItemsMultiple : public TuplePatternItems { @@ -1113,7 +1113,7 @@ public: return *this; } - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } void accept_vis (HIRFullVisitor &vis) override; @@ -1122,6 +1122,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::TUPLE; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1170,7 +1175,7 @@ public: GroupedPattern (GroupedPattern &&other) = default; GroupedPattern &operator= (GroupedPattern &&other) = default; - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } void accept_vis (HIRFullVisitor &vis) override; @@ -1179,6 +1184,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::GROUPED; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1229,7 +1239,7 @@ public: SlicePattern (SlicePattern &&other) = default; SlicePattern &operator= (SlicePattern &&other) = default; - Location get_locus () const { return locus; } + Location get_locus () const override { return locus; } void accept_vis (HIRFullVisitor &vis) override; @@ -1238,6 +1248,11 @@ public: return mappings; } + PatternType get_pattern_type () const override final + { + return PatternType::SLICE; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 17d207a..532b95b 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -314,6 +314,21 @@ protected: class Pattern { public: + enum PatternType + { + PATH, + LITERAL, + IDENTIFIER, + WILDCARD, + RANGE, + REFERENCE, + STRUCT, + TUPLE_STRUCT, + TUPLE, + GROUPED, + SLICE, + }; + // Unique pointer custom clone function std::unique_ptr<Pattern> clone_pattern () const { @@ -332,6 +347,8 @@ public: virtual Location get_locus () const = 0; + virtual PatternType get_pattern_type () const = 0; + protected: // Clone pattern implementation as pure virtual method virtual Pattern *clone_pattern_impl () const = 0; diff --git a/gcc/rust/resolve/rust-ast-resolve-pattern.h b/gcc/rust/resolve/rust-ast-resolve-pattern.h index 57c73ba..01a0534 100644 --- a/gcc/rust/resolve/rust-ast-resolve-pattern.h +++ b/gcc/rust/resolve/rust-ast-resolve-pattern.h @@ -83,6 +83,17 @@ public: pattern.get_is_mut ()); } + void visit (AST::WildcardPattern &pattern) override + { + resolver->get_name_scope ().insert ( + CanonicalPath::new_seg (pattern.get_node_id (), "_"), + pattern.get_node_id (), pattern.get_locus ()); + resolver->insert_new_definition (pattern.get_node_id (), + Definition{pattern.get_node_id (), + parent}); + resolver->mark_decl_mutability (pattern.get_node_id (), false); + } + // cases in a match expression void visit (AST::PathInExpression &pattern) override; diff --git a/gcc/testsuite/rust/compile/issue-557.rs b/gcc/testsuite/rust/compile/issue-557.rs new file mode 100644 index 0000000..aeb5ba6 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-557.rs @@ -0,0 +1,4 @@ +// { dg-additional-options "-w" } +fn test(a: i32, _: i32) { + let _ = 42 + a; +} |