You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #86492 - hyd-dev:no-mangle-method, r=petrochenkov
Associated functions that contain extern indicator or have `#[rustc_std_internal_symbol]` are reachable
Previously these fails to link with ``undefined reference to `foo'``:
<details>
<summary>Example 1</summary>
```rs
struct AssocFn;
impl AssocFn {
#[no_mangle]
fn foo() {}
}
fn main() {
extern "Rust" {
fn foo();
}
unsafe { foo() }
}
```
([Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f1244afcdd26e2a28445f6e82ca46b50))
</details>
<details>
<summary>Example 2</summary>
```rs
#![crate_name = "lib"]
#![crate_type = "lib"]
struct AssocFn;
impl AssocFn {
#[no_mangle]
fn foo() {}
}
```
```rs
extern crate lib;
fn main() {
extern "Rust" {
fn foo();
}
unsafe { foo() }
}
```
</details>
But I believe they should link successfully, because this works:
<details>
```rs
#[no_mangle]
fn foo() {}
fn main() {
extern "Rust" {
fn foo();
}
unsafe { foo() }
}
```
([Playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=789b3f283ee6126f53939429103ed98d))
</details>
This PR fixes the problem, by adding associated functions that have "custom linkage" to `reachable_set`, just like normal functions.
I haven't tested whether #76211 and [Miri](rust-lang/miri#1837) are fixed by this PR yet, but I'm submitting this anyway since this fixes the examples above.
I added a `run-pass` test that combines my two examples above, but I'm not sure if that's the right way to test this. Maybe I should add / modify an existing codegen test (`src/test/codegen/export-no-mangle.rs`?) instead?
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
21
21
22
+
error: declaration of a `no_mangle` method
23
+
--> $DIR/lint-unsafe-code.rs:41:5
24
+
|
25
+
LL | #[no_mangle] fn foo() {}
26
+
| ^^^^^^^^^^^^
27
+
|
28
+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
29
+
30
+
error: declaration of a `no_mangle` method
31
+
--> $DIR/lint-unsafe-code.rs:45:5
32
+
|
33
+
LL | #[no_mangle] fn foo() {}
34
+
| ^^^^^^^^^^^^
35
+
|
36
+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
37
+
22
38
error: declaration of a function with `export_name`
23
-
--> $DIR/lint-unsafe-code.rs:34:1
39
+
--> $DIR/lint-unsafe-code.rs:48:1
24
40
|
25
41
LL | #[export_name = "bar"] fn bar() {}
26
42
| ^^^^^^^^^^^^^^^^^^^^^^
27
43
|
28
44
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
29
45
30
46
error: declaration of a static with `export_name`
31
-
--> $DIR/lint-unsafe-code.rs:35:1
47
+
--> $DIR/lint-unsafe-code.rs:49:1
32
48
|
33
49
LL | #[export_name = "BAR"] static BAR: u32 = 5;
34
50
| ^^^^^^^^^^^^^^^^^^^^^^
35
51
|
36
52
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
37
53
54
+
error: declaration of a method with `export_name`
55
+
--> $DIR/lint-unsafe-code.rs:54:5
56
+
|
57
+
LL | #[export_name = "bar"] fn bar() {}
58
+
| ^^^^^^^^^^^^^^^^^^^^^^
59
+
|
60
+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
61
+
62
+
error: declaration of a method with `export_name`
63
+
--> $DIR/lint-unsafe-code.rs:58:5
64
+
|
65
+
LL | #[export_name = "bar"] fn foo() {}
66
+
| ^^^^^^^^^^^^^^^^^^^^^^
67
+
|
68
+
= note: the linker's behavior with multiple libraries exporting duplicate symbol names is undefined and Rust cannot provide guarantees when you manually override them
69
+
38
70
error: declaration of an `unsafe` function
39
-
--> $DIR/lint-unsafe-code.rs:37:1
71
+
--> $DIR/lint-unsafe-code.rs:61:1
40
72
|
41
73
LL | unsafe fn baz() {}
42
74
| ^^^^^^^^^^^^^^^^^^
43
75
44
76
error: declaration of an `unsafe` trait
45
-
--> $DIR/lint-unsafe-code.rs:38:1
77
+
--> $DIR/lint-unsafe-code.rs:62:1
46
78
|
47
79
LL | unsafe trait Foo {}
48
80
| ^^^^^^^^^^^^^^^^^^^
49
81
50
82
error: implementation of an `unsafe` trait
51
-
--> $DIR/lint-unsafe-code.rs:39:1
83
+
--> $DIR/lint-unsafe-code.rs:63:1
52
84
|
53
85
LL | unsafe impl Foo for Bar {}
54
86
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
55
87
56
88
error: declaration of an `unsafe` method
57
-
--> $DIR/lint-unsafe-code.rs:42:5
89
+
--> $DIR/lint-unsafe-code.rs:66:5
58
90
|
59
91
LL | unsafe fn baz(&self);
60
92
| ^^^^^^^^^^^^^^^^^^^^^
61
93
62
94
error: implementation of an `unsafe` method
63
-
--> $DIR/lint-unsafe-code.rs:43:5
95
+
--> $DIR/lint-unsafe-code.rs:67:5
64
96
|
65
97
LL | unsafe fn provided(&self) {}
66
98
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67
99
68
100
error: implementation of an `unsafe` method
69
-
--> $DIR/lint-unsafe-code.rs:44:5
101
+
--> $DIR/lint-unsafe-code.rs:68:5
70
102
|
71
103
LL | unsafe fn provided_override(&self) {}
72
104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73
105
74
106
error: implementation of an `unsafe` method
75
-
--> $DIR/lint-unsafe-code.rs:48:5
107
+
--> $DIR/lint-unsafe-code.rs:72:5
76
108
|
77
109
LL | unsafe fn baz(&self) {}
78
110
| ^^^^^^^^^^^^^^^^^^^^^^^
79
111
80
112
error: implementation of an `unsafe` method
81
-
--> $DIR/lint-unsafe-code.rs:49:5
113
+
--> $DIR/lint-unsafe-code.rs:73:5
82
114
|
83
115
LL | unsafe fn provided_override(&self) {}
84
116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85
117
86
118
error: implementation of an `unsafe` method
87
-
--> $DIR/lint-unsafe-code.rs:68:5
119
+
--> $DIR/lint-unsafe-code.rs:92:5
88
120
|
89
121
LL | unsafe fn provided_override(&self) {}
90
122
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91
123
92
124
error: implementation of an `unsafe` method
93
-
--> $DIR/lint-unsafe-code.rs:79:5
125
+
--> $DIR/lint-unsafe-code.rs:103:5
94
126
|
95
127
LL | unsafe fn provided(&self) {}
96
128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
97
129
98
130
error: implementation of an `unsafe` method
99
-
--> $DIR/lint-unsafe-code.rs:85:5
131
+
--> $DIR/lint-unsafe-code.rs:109:5
100
132
|
101
133
LL | unsafe fn provided(&self) {}
102
134
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103
135
104
136
error: implementation of an `unsafe` method
105
-
--> $DIR/lint-unsafe-code.rs:89:5
137
+
--> $DIR/lint-unsafe-code.rs:113:5
106
138
|
107
139
LL | unsafe fn baz(&self) {}
108
140
| ^^^^^^^^^^^^^^^^^^^^^^^
109
141
110
142
error: usage of an `unsafe` block
111
-
--> $DIR/lint-unsafe-code.rs:100:5
143
+
--> $DIR/lint-unsafe-code.rs:124:5
112
144
|
113
145
LL | unsafe {}
114
146
| ^^^^^^^^^
@@ -172,5 +204,5 @@ LL | unsafe_in_macro!()
172
204
|
173
205
= note: this error originates in the macro `unsafe_in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
0 commit comments