Skip to content

Commit dda10f2

Browse files
committed
Add more tests, fix span issue, improve diagnostics.
1 parent eb49245 commit dda10f2

7 files changed

+96
-4
lines changed

src/librustc_typeck/collect.rs

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

25622562
let mut inline_span = None;
2563+
let mut link_ordinal_span = None;
25632564
for attr in attrs.iter() {
25642565
if attr.check_name(sym::cold) {
25652566
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
@@ -2642,6 +2643,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26422643
} else if attr.check_name(sym::link_name) {
26432644
codegen_fn_attrs.link_name = attr.value_str();
26442645
} else if attr.check_name(sym::link_ordinal) {
2646+
link_ordinal_span = Some(attr.span);
26452647
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
26462648
codegen_fn_attrs.link_ordinal = ordinal;
26472649
}
@@ -2747,7 +2749,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
27472749
codegen_fn_attrs.export_name = Some(name);
27482750
codegen_fn_attrs.link_name = Some(name);
27492751
}
2750-
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, inline_span);
2752+
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
27512753

27522754
// Internal symbols to the standard library all have no_mangle semantics in
27532755
// that they have defined symbol names present in the function name. This
@@ -2772,14 +2774,18 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<usize> {
27722774
Some(*ordinal as usize)
27732775
} else {
27742776
let msg = format!(
2775-
"too large ordinal value in link_ordinal value: `{}`",
2777+
"ordinal value in `link_ordinal` is too large: `{}`",
27762778
&ordinal
27772779
);
2778-
tcx.sess.span_err(attr.span, &msg);
2780+
tcx.sess.struct_span_err(attr.span, &msg)
2781+
.note("the value may not exceed `std::usize::MAX`")
2782+
.emit();
27792783
None
27802784
}
27812785
} else {
2782-
tcx.sess.span_err(attr.span, "illegal ordinal format in link_ordinal");
2786+
tcx.sess.struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2787+
.note("an unsuffixed integer value, e.g., `1`, is expected")
2788+
.emit();
27832789
None
27842790
}
27852791
}
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)