From 4600212a385b41f0c718b6b07dbb6c2c4531314a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 29 Aug 2013 22:29:26 -0700 Subject: [PATCH] Fix inner statics having the same symbol name Before, the path name for all items defined in methods of traits and impls never took into account the name of the method. This meant that if you had two statics of the same name in two different methods the statics would end up having the same symbol named (even after mangling) because the path components leading to the symbol were exactly the same (just __extensions__ and the static name). It turns out that if you add the symbol "A" twice to LLVM, it automatically makes the second one "A1" instead of "A". What this meant is that in local crate compilations we never found this bug. Even across crates, this was never a problem. The problem arises when you have generic methods that don't get generated at compile-time of a library. If the statics were re-added to LLVM by a client crate of a library in a different order, you would reference different constants (the integer suffixes wouldn't be guaranteed to be the same). This fixes the problem by adding the method name to symbol path when building the ast_map. In doing so, two symbols in two different methods are disambiguated against. --- src/libsyntax/ast_map.rs | 8 ++++++++ src/test/auxiliary/inner_static.rs | 28 ++++++++++++++++++++++++++++ src/test/run-pass/inner-static.rs | 19 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/test/auxiliary/inner_static.rs create mode 100644 src/test/run-pass/inner-static.rs diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs index 6e022e9804b4..5da94bebf165 100644 --- a/src/libsyntax/ast_map.rs +++ b/src/libsyntax/ast_map.rs @@ -153,7 +153,15 @@ impl Ctx { for a in decl.inputs.iter() { self.map.insert(a.id, node_arg); } + match *fk { + visit::fk_method(name, _, _) => { self.path.push(path_name(name)) } + _ => {} + } visit::walk_fn(self, fk, decl, body, sp, id, ()); + match *fk { + visit::fk_method(*) => { self.path.pop(); } + _ => {} + } } fn map_stmt(&mut self, stmt: @stmt) { diff --git a/src/test/auxiliary/inner_static.rs b/src/test/auxiliary/inner_static.rs new file mode 100644 index 000000000000..706586684ec0 --- /dev/null +++ b/src/test/auxiliary/inner_static.rs @@ -0,0 +1,28 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub struct A; + +impl A { + pub fn foo(&self) -> int { + static a: int = 5; + return a + } + + pub fn bar(&self) -> int { + static a: int = 3; + return a; + } +} + +pub fn foo() -> int { + let a = A::<()>; + return a.foo() + a.bar(); +} diff --git a/src/test/run-pass/inner-static.rs b/src/test/run-pass/inner-static.rs new file mode 100644 index 000000000000..969dc607b801 --- /dev/null +++ b/src/test/run-pass/inner-static.rs @@ -0,0 +1,19 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:inner_static.rs +// xfail-fast + +extern mod inner_static; + +pub fn main() { + let a = inner_static::A::<()>; + assert_eq!(a.bar(), 3); +}