From 380c8295051c6b42adb4f703268c7465aed57c44 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Sat, 27 Mar 2021 16:28:45 +0000 Subject: Init NodeId for NodeMappings on HIR::MethodCallExpr We resolve methods during type resolution since it depends on the receiver type being used. To ensure the function is marked as used we must then mark the method name as resolved, this is also used in the backend GENERIC compilation to lookup the function definition to be compiled. When this was unset multiple method calls would fail. Fixes #310 --- gcc/rust/hir/rust-ast-lower-expr.h | 6 ++--- gcc/testsuite/rust.test/compile/methods3.rs | 41 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/rust.test/compile/methods3.rs (limited to 'gcc') diff --git a/gcc/rust/hir/rust-ast-lower-expr.h b/gcc/rust/hir/rust-ast-lower-expr.h index c55213d..b003334 100644 --- a/gcc/rust/hir/rust-ast-lower-expr.h +++ b/gcc/rust/hir/rust-ast-lower-expr.h @@ -241,9 +241,9 @@ public: }); auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping ( - crate_num, UNKNOWN_NODEID /* this can map back to the AST*/, - mappings->get_next_hir_id (crate_num), UNKNOWN_LOCAL_DEFID); + Analysis::NodeMapping mapping (crate_num, expr.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); translated = new HIR::MethodCallExpr (mapping, std::unique_ptr (receiver), diff --git a/gcc/testsuite/rust.test/compile/methods3.rs b/gcc/testsuite/rust.test/compile/methods3.rs new file mode 100644 index 0000000..a71d045 --- /dev/null +++ b/gcc/testsuite/rust.test/compile/methods3.rs @@ -0,0 +1,41 @@ +struct Point { + x: f64, + y: f64, +} + +impl Point { + fn origin() -> Point { + Point { x: 0.0, y: 0.0 } + } + + fn new(x: f64, y: f64) -> Point { + Point { x: x, y: y } + } +} + +struct Rectangle { + p1: Point, + p2: Point, +} + +impl Rectangle { + fn from(p1: Point, p2: Point) -> Self { + Self { p1, p2 } + } + + fn sum_x(self) -> f64 { + let p1 = self.p1; + let p2 = self.p2; + p1.x + p2.x + } +} + +fn main() { + let p1 = Point::origin(); + let p2 = Point::new(3.0, 4.0); + let rect = Rectangle::from(p1, p2); + + let sum = rect.sum_x(); + // multiple MethodCallExpr were causing issue #310 + let sum = rect.sum_x(); +} -- cgit v1.1