Skip to content

Commit d77da9d

Browse files
committed
Auto merge of #100073 - dpaoliello:externvar, r=michaelwoerister
Add test for raw-dylib with an external variable All existing tests of link kind `raw-dylib` only validate the ability to link against functions, but it is also possible to link against variables. This adds tests for linking against a variable using `raw-dylib` both by-name and by-ordinal.
2 parents 9bbbf60 + 0a754b3 commit d77da9d

22 files changed

+133
-9
lines changed

compiler/rustc_passes/src/check_attr.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,14 @@ impl CheckAttrVisitor<'_> {
209209
}
210210

211211
// FIXME(@lcnr): this doesn't belong here.
212-
if matches!(target, Target::Closure | Target::Fn | Target::Method(_) | Target::ForeignFn) {
212+
if matches!(
213+
target,
214+
Target::Closure
215+
| Target::Fn
216+
| Target::Method(_)
217+
| Target::ForeignFn
218+
| Target::ForeignStatic
219+
) {
213220
self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
214221
}
215222

src/test/run-make/raw-dylib-c/extern_1.c

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <stdio.h>
22

3+
__declspec(dllexport) int extern_variable = 0;
4+
35
__declspec(dllexport) void extern_fn_1() {
46
printf("extern_fn_1\n");
57
fflush(stdout);
@@ -10,6 +12,11 @@ __declspec(dllexport) void extern_fn_2() {
1012
fflush(stdout);
1113
}
1214

15+
__declspec(dllexport) void print_extern_variable() {
16+
printf("extern_variable value: %d\n", extern_variable);
17+
fflush(stdout);
18+
}
19+
1320
__declspec(dllexport) void extern_fn_with_long_name() {
1421
printf("extern_fn_with_long_name; got the rename\n");
1522
fflush(stdout);

src/test/run-make/raw-dylib-c/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ extern {
1212

1313
pub fn library_function() {
1414
#[link(name = "extern_1", kind = "raw-dylib")]
15-
extern { fn extern_fn_2(); }
15+
extern {
16+
fn extern_fn_2();
17+
fn print_extern_variable();
18+
static mut extern_variable: i32;
19+
}
1620

1721
unsafe {
1822
extern_fn_1();
1923
extern_fn_2();
2024
extern_fn_3();
25+
extern_variable = 42;
26+
print_extern_variable();
27+
extern_variable = -42;
28+
print_extern_variable();
2129
}
2230
}
2331

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
extern_fn_1
22
extern_fn_2; didn't get the rename
33
extern_fn_3
4+
extern_variable value: 42
5+
extern_variable value: -42

src/test/run-make/raw-dylib-link-ordinal/exporter.c

+7
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@
33
void exported_function() {
44
printf("exported_function\n");
55
}
6+
7+
int exported_variable = 0;
8+
9+
void print_exported_variable() {
10+
printf("exported_variable value: %d\n", exported_variable);
11+
fflush(stdout);
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
LIBRARY exporter
22
EXPORTS
33
exported_function @13 NONAME
4+
exported_variable @5 NONAME
5+
print_exported_variable @9 NONAME

src/test/run-make/raw-dylib-link-ordinal/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
extern {
55
#[link_ordinal(13)]
66
fn imported_function();
7+
#[link_ordinal(5)]
8+
static mut imported_variable: i32;
9+
#[link_ordinal(9)]
10+
fn print_imported_variable();
711
}
812

913
pub fn library_function() {
1014
unsafe {
1115
imported_function();
16+
imported_variable = 42;
17+
print_imported_variable();
18+
imported_variable = -42;
19+
print_imported_variable();
1220
}
1321
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
exported_function
2+
exported_variable value: 42
3+
exported_variable value: -42

src/test/ui/feature-gates/feature-gate-raw-dylib-2.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ extern "C" {
33
#[link_ordinal(42)]
44
//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature
55
fn foo();
6+
#[link_ordinal(5)]
7+
//~^ ERROR: the `#[link_ordinal]` attribute is an experimental feature
8+
static mut imported_variable: i32;
69
}
710

811
fn main() {}

src/test/ui/feature-gates/feature-gate-raw-dylib-2.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ LL | #[link_ordinal(42)]
77
= note: see issue #58713 <https://github.com/rust-lang/rust/issues/58713> for more information
88
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable
99

10-
error: aborting due to previous error
10+
error[E0658]: the `#[link_ordinal]` attribute is an experimental feature
11+
--> $DIR/feature-gate-raw-dylib-2.rs:6:5
12+
|
13+
LL | #[link_ordinal(5)]
14+
| ^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #58713 <https://github.com/rust-lang/rust/issues/58713> for more information
17+
= help: add `#![feature(raw_dylib)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
1120

1221
For more information about this error, try `rustc --explain E0658`.

src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ extern "C" {
77
#[link_ordinal(42)]
88
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
99
fn foo();
10+
#[link_name="foo"]
11+
#[link_ordinal(5)]
12+
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
13+
static mut imported_variable: i32;
1014
}
1115

1216
fn main() {}

src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,11 @@ error: cannot use `#[link_name]` with `#[link_ordinal]`
1313
LL | #[link_ordinal(42)]
1414
| ^^^^^^^^^^^^^^^^^^^
1515

16-
error: aborting due to previous error; 1 warning emitted
16+
error: cannot use `#[link_name]` with `#[link_ordinal]`
17+
--> $DIR/link-ordinal-and-name.rs:11:5
18+
|
19+
LL | #[link_ordinal(5)]
20+
| ^^^^^^^^^^^^^^^^^^
21+
22+
error: aborting due to 2 previous errors; 1 warning emitted
1723

src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ extern "C" {
66
#[link_ordinal("JustMonika")]
77
//~^ ERROR illegal ordinal format in `link_ordinal`
88
fn foo();
9+
#[link_ordinal("JustMonika")]
10+
//~^ ERROR illegal ordinal format in `link_ordinal`
11+
static mut imported_variable: i32;
912
}
1013

1114
fn main() {}

src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ LL | #[link_ordinal("JustMonika")]
1515
|
1616
= note: an unsuffixed integer value, e.g., `1`, is expected
1717

18-
error: aborting due to previous error; 1 warning emitted
18+
error: illegal ordinal format in `link_ordinal`
19+
--> $DIR/link-ordinal-invalid-format.rs:9:5
20+
|
21+
LL | #[link_ordinal("JustMonika")]
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: an unsuffixed integer value, e.g., `1`, is expected
25+
26+
error: aborting due to 2 previous errors; 1 warning emitted
1927

src/test/ui/rfc-2627-raw-dylib/link-ordinal-missing-argument.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ extern "C" {
66
#[link_ordinal()]
77
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
88
fn foo();
9+
#[link_ordinal()]
10+
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
11+
static mut imported_variable: i32;
912
}
1013

1114
fn main() {}

src/test/ui/rfc-2627-raw-dylib/link-ordinal-missing-argument.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ LL | #[link_ordinal()]
1515
|
1616
= note: the attribute requires exactly one argument
1717

18-
error: aborting due to previous error; 1 warning emitted
18+
error: incorrect number of arguments to `#[link_ordinal]`
19+
--> $DIR/link-ordinal-missing-argument.rs:9:5
20+
|
21+
LL | #[link_ordinal()]
22+
| ^^^^^^^^^^^^^^^^^
23+
|
24+
= note: the attribute requires exactly one argument
25+
26+
error: aborting due to 2 previous errors; 1 warning emitted
1927

src/test/ui/rfc-2627-raw-dylib/link-ordinal-multiple.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ extern "C" {
77
#[link_ordinal(1)] //~ ERROR multiple `link_ordinal` attributes
88
#[link_ordinal(2)]
99
fn foo();
10+
#[link_ordinal(1)] //~ ERROR multiple `link_ordinal` attributes
11+
#[link_ordinal(2)]
12+
static mut imported_variable: i32;
1013
}
1114

1215
fn main() {}

src/test/ui/rfc-2627-raw-dylib/link-ordinal-multiple.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,17 @@ note: attribute also specified here
1919
LL | #[link_ordinal(2)]
2020
| ^^^^^^^^^^^^^^^^^^
2121

22-
error: aborting due to previous error; 1 warning emitted
22+
error: multiple `link_ordinal` attributes
23+
--> $DIR/link-ordinal-multiple.rs:10:5
24+
|
25+
LL | #[link_ordinal(1)]
26+
| ^^^^^^^^^^^^^^^^^^ help: remove this attribute
27+
|
28+
note: attribute also specified here
29+
--> $DIR/link-ordinal-multiple.rs:11:5
30+
|
31+
LL | #[link_ordinal(2)]
32+
| ^^^^^^^^^^^^^^^^^^
33+
34+
error: aborting due to 2 previous errors; 1 warning emitted
2335

src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ extern "C" {
66
#[link_ordinal(72436)]
77
//~^ ERROR ordinal value in `link_ordinal` is too large: `72436`
88
fn foo();
9+
#[link_ordinal(72436)]
10+
//~^ ERROR ordinal value in `link_ordinal` is too large: `72436`
11+
static mut imported_variable: i32;
912
}
1013

1114
fn main() {}

src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ LL | #[link_ordinal(72436)]
1515
|
1616
= note: the value may not exceed `u16::MAX`
1717

18-
error: aborting due to previous error; 1 warning emitted
18+
error: ordinal value in `link_ordinal` is too large: `72436`
19+
--> $DIR/link-ordinal-too-large.rs:9:5
20+
|
21+
LL | #[link_ordinal(72436)]
22+
| ^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: the value may not exceed `u16::MAX`
25+
26+
error: aborting due to 2 previous errors; 1 warning emitted
1927

src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ extern "C" {
66
#[link_ordinal(3, 4)]
77
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
88
fn foo();
9+
#[link_ordinal(3, 4)]
10+
//~^ ERROR incorrect number of arguments to `#[link_ordinal]`
11+
static mut imported_variable: i32;
912
}
1013

1114
fn main() {}

src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-many-arguments.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@ LL | #[link_ordinal(3, 4)]
1515
|
1616
= note: the attribute requires exactly one argument
1717

18-
error: aborting due to previous error; 1 warning emitted
18+
error: incorrect number of arguments to `#[link_ordinal]`
19+
--> $DIR/link-ordinal-too-many-arguments.rs:9:5
20+
|
21+
LL | #[link_ordinal(3, 4)]
22+
| ^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: the attribute requires exactly one argument
25+
26+
error: aborting due to 2 previous errors; 1 warning emitted
1927

0 commit comments

Comments
 (0)