Skip to content

Commit 9846af8

Browse files
committed
Add more tests, fix span issue, improve diagnostics.
1 parent 6e9c0b0 commit 9846af8

7 files changed

+96
-4
lines changed

src/librustc_typeck/collect.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -2522,6 +2522,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
25222522
let whitelist = tcx.target_features_whitelist(LOCAL_CRATE);
25232523

25242524
let mut inline_span = None;
2525+
let mut link_ordinal_span = None;
25252526
for attr in attrs.iter() {
25262527
if attr.check_name(sym::cold) {
25272528
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
@@ -2604,6 +2605,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26042605
} else if attr.check_name(sym::link_name) {
26052606
codegen_fn_attrs.link_name = attr.value_str();
26062607
} else if attr.check_name(sym::link_ordinal) {
2608+
link_ordinal_span = Some(attr.span);
26072609
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
26082610
codegen_fn_attrs.link_ordinal = ordinal;
26092611
}
@@ -2709,7 +2711,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27092711
codegen_fn_attrs.export_name = Some(name);
27102712
codegen_fn_attrs.link_name = Some(name);
27112713
}
2712-
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, inline_span);
2714+
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
27132715

27142716
// Internal symbols to the standard library all have no_mangle semantics in
27152717
// that they have defined symbol names present in the function name. This
@@ -2734,14 +2736,18 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<usize> {
27342736
Some(*ordinal as usize)
27352737
} else {
27362738
let msg = format!(
2737-
"too large ordinal value in link_ordinal value: `{}`",
2739+
"ordinal value in `link_ordinal` is too large: `{}`",
27382740
&ordinal
27392741
);
2740-
tcx.sess.span_err(attr.span, &msg);
2742+
tcx.sess.struct_span_err(attr.span, &msg)
2743+
.note("the value may not exceed `std::usize::MAX`")
2744+
.emit();
27412745
None
27422746
}
27432747
} else {
2744-
tcx.sess.span_err(attr.span, "illegal ordinal format in link_ordinal");
2748+
tcx.sess.struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2749+
.note("an unsuffixed integer value, e.g., `1`, is expected")
2750+
.emit();
27452751
None
27462752
}
27472753
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash
3+
4+
#[link(name="foo")]
5+
extern {
6+
#[link_name="foo"]
7+
#[link_ordinal(42)]
8+
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
9+
fn foo();
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
2+
--> $DIR/link-ordinal-and-name.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error: cannot use `#[link_name]` with `#[link_ordinal]`
10+
--> $DIR/link-ordinal-and-name.rs:7:5
11+
|
12+
LL | #[link_ordinal(42)]
13+
| ^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash
3+
4+
#[link(name="foo")]
5+
extern {
6+
#[link_ordinal("JustMonika")]
7+
//~^ ERROR illegal ordinal format in `link_ordinal`
8+
fn foo();
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
2+
--> $DIR/link-ordinal-invalid-format.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error: illegal ordinal format in `link_ordinal`
10+
--> $DIR/link-ordinal-invalid-format.rs:6:5
11+
|
12+
LL | #[link_ordinal("JustMonika")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: an unsuffixed integer value, e.g., `1`, is expected
16+
17+
error: aborting due to previous error
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(raw_dylib)]
2+
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash
3+
4+
#[link(name="foo")]
5+
extern {
6+
#[link_ordinal(18446744073709551616)]
7+
//~^ ERROR ordinal value in `link_ordinal` is too large: `18446744073709551616`
8+
fn foo();
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
2+
--> $DIR/link-ordinal-too-large.rs:1:12
3+
|
4+
LL | #![feature(raw_dylib)]
5+
| ^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
9+
error: ordinal value in `link_ordinal` is too large: `18446744073709551616`
10+
--> $DIR/link-ordinal-too-large.rs:6:5
11+
|
12+
LL | #[link_ordinal(18446744073709551616)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: the value may not exceed `std::usize::MAX`
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)