diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-04-29 13:56:37 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-04-29 14:44:51 +0100 |
commit | c1296bc68e0b94285bbbab89a63ae44520d812ac (patch) | |
tree | 869f72d6f48e8ce65621fff22fb03cf8329a99ef /gcc | |
parent | eb2c6124c46d0b3d58630c80af007ed092adc1ea (diff) | |
download | gcc-c1296bc68e0b94285bbbab89a63ae44520d812ac.zip gcc-c1296bc68e0b94285bbbab89a63ae44520d812ac.tar.gz gcc-c1296bc68e0b94285bbbab89a63ae44520d812ac.tar.bz2 |
Add HIR lowering for GenericBindingArgs
We need to lower the bindings for defaults in the GenericArgs structure.
We can also take advantage of RichLocations further improving the
diagnostic errors here.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-path.h | 4 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-base.h | 2 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 17 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 6 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.cc | 17 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tyty.h | 2 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/xfail_compile/type-bindings1.rs | 8 |
7 files changed, 51 insertions, 5 deletions
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h index c8542d9..86da203 100644 --- a/gcc/rust/ast/rust-path.h +++ b/gcc/rust/ast/rust-path.h @@ -124,6 +124,10 @@ public: rust_assert (type != nullptr); return type; } + + Location get_locus () const { return locus; } + + Identifier get_identifier () const { return identifier; } }; // Generic arguments allowed in each path expression segment - inline? diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index b127ec4..16143d2 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -291,6 +291,8 @@ protected: HIR::PathExprSegment lower_path_expr_seg (AST::PathExprSegment &s); HIR::GenericArgs lower_generic_args (AST::GenericArgs &args); + + HIR::GenericArgsBinding lower_binding (AST::GenericArgsBinding &binding); }; } // namespace HIR diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index 7ba5d32..f1129ab 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -314,10 +314,25 @@ ASTLoweringBase::lower_path_expr_seg (AST::PathExprSegment &s) : HIR::GenericArgs::create_empty ()); } +HIR::GenericArgsBinding +ASTLoweringBase::lower_binding (AST::GenericArgsBinding &binding) +{ + HIR::Type *lowered_type + = ASTLoweringType::translate (binding.get_type ().get ()); + return HIR::GenericArgsBinding (binding.get_identifier (), + std::unique_ptr<HIR::Type> (lowered_type), + binding.get_locus ()); +} + HIR::GenericArgs ASTLoweringBase::lower_generic_args (AST::GenericArgs &args) { - std::vector<HIR::GenericArgsBinding> binding_args; // TODO + std::vector<HIR::GenericArgsBinding> binding_args; + for (auto &binding : args.get_binding_args ()) + { + HIR::GenericArgsBinding b = lower_binding (binding); + binding_args.push_back (std::move (b)); + } std::vector<HIR::Lifetime> lifetime_args; for (auto &lifetime : args.get_lifetime_args ()) diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 262a04a..c163487 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -107,6 +107,12 @@ public: GenericArgsBinding &operator= (GenericArgsBinding &&other) = default; std::string as_string () const; + + Identifier get_identifier () const { return identifier; } + + std::unique_ptr<Type> &get_type () { return type; } + + Location get_locus () const { return locus; } }; // Generic arguments allowed in each path expression segment - inline? diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc index c8f5f19..be56dc7 100644 --- a/gcc/rust/typecheck/rust-tyty.cc +++ b/gcc/rust/typecheck/rust-tyty.cc @@ -213,15 +213,21 @@ SubstitutionRef::get_mappings_from_generic_args (HIR::GenericArgs &args) { if (args.get_binding_args ().size () > 0) { - rust_error_at (args.get_locus (), - "associated type bindings are not allowed here"); + RichLocation r (args.get_locus ()); + for (auto &binding : args.get_binding_args ()) + r.add_range (binding.get_locus ()); + + rust_error_at (r, "associated type bindings are not allowed here"); return SubstitutionArgumentMappings::error (); } if (args.get_type_args ().size () > substitutions.size ()) { + RichLocation r (args.get_locus ()); + r.add_range (substitutions.front ().get_param_locus ()); + rust_error_at ( - args.get_locus (), + r, "generic item takes at most %lu type arguments but %lu were supplied", substitutions.size (), args.get_type_args ().size ()); return SubstitutionArgumentMappings::error (); @@ -229,8 +235,11 @@ SubstitutionRef::get_mappings_from_generic_args (HIR::GenericArgs &args) if (args.get_type_args ().size () < substitutions.size ()) { + RichLocation r (args.get_locus ()); + r.add_range (substitutions.front ().get_param_locus ()); + rust_error_at ( - args.get_locus (), + r, "generic item takes at least %lu type arguments but %lu were supplied", substitutions.size (), args.get_type_args ().size ()); return SubstitutionArgumentMappings::error (); diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h index bcc694b..91c3f06 100644 --- a/gcc/rust/typecheck/rust-tyty.h +++ b/gcc/rust/typecheck/rust-tyty.h @@ -444,6 +444,8 @@ public: return p->resolve ()->get_kind () == TypeKind::PARAM; } + Location get_param_locus () const { return generic->get_locus_slow (); } + private: std::unique_ptr<HIR::GenericParam> &generic; ParamType *param; diff --git a/gcc/testsuite/rust.test/xfail_compile/type-bindings1.rs b/gcc/testsuite/rust.test/xfail_compile/type-bindings1.rs new file mode 100644 index 0000000..5e00f87 --- /dev/null +++ b/gcc/testsuite/rust.test/xfail_compile/type-bindings1.rs @@ -0,0 +1,8 @@ +// { dg-excess-errors "Noisy error and debug" } +struct Foo<A, B>(A, B); + +fn main() { + let a; + a = Foo::<A = i32, B = f32>(123f32); + // { dg-error "associated type bindings are not allowed here" "" { target { *-*-* } } .-1 } +} |