From a9931e9c4f4f74ca90e34871cfd093b04169eec2 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Tue, 15 Oct 2024 23:16:23 -0400 Subject: [PATCH] Disambiguate generic args during name resolution 2.0 gcc/rust/ChangeLog: * resolve/rust-late-name-resolver-2.0.cc (Late::visit): Visit GenericArgs and GenericArg, the former because the latter involves a non-virtual member function call. * resolve/rust-late-name-resolver-2.0.h (Late::visit): Likewise. Signed-off-by: Owen Avery --- .../resolve/rust-late-name-resolver-2.0.cc | 32 +++++++++++++++++++ .../resolve/rust-late-name-resolver-2.0.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index 369451bed0e..0fe2902f2f8 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -275,5 +275,37 @@ Late::visit (AST::StructExprStructFields &s) DefaultResolver::visit (s); } +// needed because Late::visit (AST::GenericArg &) is non-virtual +void +Late::visit (AST::GenericArgs &args) +{ + for (auto &lifetime : args.get_lifetime_args ()) + visit (lifetime); + + for (auto &generic : args.get_generic_args ()) + visit (generic); + + for (auto &binding : args.get_binding_args ()) + visit (binding); +} + +void +Late::visit (AST::GenericArg &arg) +{ + if (arg.get_kind () == AST::GenericArg::Kind::Either) + { + // prefer type parameter to const parameter on ambiguity + auto type = ctx.types.get (arg.get_path ()); + auto value = ctx.values.get (arg.get_path ()); + + if (!type.has_value () && value.has_value ()) + arg = arg.disambiguate_to_const (); + else + arg = arg.disambiguate_to_type (); + } + + DefaultResolver::visit (arg); +} + } // namespace Resolver2_0 } // namespace Rust diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h b/gcc/rust/resolve/rust-late-name-resolver-2.0.h index b02101a1f3a..1cdf2ec3f60 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h @@ -49,6 +49,8 @@ class Late : public DefaultResolver void visit (AST::StructExprStructBase &) override; void visit (AST::StructExprStructFields &) override; void visit (AST::StructStruct &) override; + void visit (AST::GenericArgs &) override; + void visit (AST::GenericArg &); private: /* Setup Rust's builtin types (u8, i32, !...) in the resolver */