Skip to content

Commit

Permalink
Add more tests, fix span issue, improve diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
crlf0710 committed Sep 20, 2019
1 parent 6e9c0b0 commit 9846af8
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2522,6 +2522,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
let whitelist = tcx.target_features_whitelist(LOCAL_CRATE);

let mut inline_span = None;
let mut link_ordinal_span = None;
for attr in attrs.iter() {
if attr.check_name(sym::cold) {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
Expand Down Expand Up @@ -2604,6 +2605,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
} else if attr.check_name(sym::link_name) {
codegen_fn_attrs.link_name = attr.value_str();
} else if attr.check_name(sym::link_ordinal) {
link_ordinal_span = Some(attr.span);
if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
codegen_fn_attrs.link_ordinal = ordinal;
}
Expand Down Expand Up @@ -2709,7 +2711,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
codegen_fn_attrs.export_name = Some(name);
codegen_fn_attrs.link_name = Some(name);
}
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, inline_span);
check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);

// Internal symbols to the standard library all have no_mangle semantics in
// that they have defined symbol names present in the function name. This
Expand All @@ -2734,14 +2736,18 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<usize> {
Some(*ordinal as usize)
} else {
let msg = format!(
"too large ordinal value in link_ordinal value: `{}`",
"ordinal value in `link_ordinal` is too large: `{}`",
&ordinal
);
tcx.sess.span_err(attr.span, &msg);
tcx.sess.struct_span_err(attr.span, &msg)
.note("the value may not exceed `std::usize::MAX`")
.emit();
None
}
} else {
tcx.sess.span_err(attr.span, "illegal ordinal format in link_ordinal");
tcx.sess.struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
.note("an unsuffixed integer value, e.g., `1`, is expected")
.emit();
None
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(raw_dylib)]
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash

#[link(name="foo")]
extern {
#[link_name="foo"]
#[link_ordinal(42)]
//~^ ERROR cannot use `#[link_name]` with `#[link_ordinal]`
fn foo();
}

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-and-name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
--> $DIR/link-ordinal-and-name.rs:1:12
|
LL | #![feature(raw_dylib)]
| ^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error: cannot use `#[link_name]` with `#[link_ordinal]`
--> $DIR/link-ordinal-and-name.rs:7:5
|
LL | #[link_ordinal(42)]
| ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

11 changes: 11 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(raw_dylib)]
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash

#[link(name="foo")]
extern {
#[link_ordinal("JustMonika")]
//~^ ERROR illegal ordinal format in `link_ordinal`
fn foo();
}

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-invalid-format.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
--> $DIR/link-ordinal-invalid-format.rs:1:12
|
LL | #![feature(raw_dylib)]
| ^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error: illegal ordinal format in `link_ordinal`
--> $DIR/link-ordinal-invalid-format.rs:6:5
|
LL | #[link_ordinal("JustMonika")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an unsuffixed integer value, e.g., `1`, is expected

error: aborting due to previous error

11 changes: 11 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(raw_dylib)]
//~^ WARN the feature `raw_dylib` is incomplete and may cause the compiler to crash

#[link(name="foo")]
extern {
#[link_ordinal(18446744073709551616)]
//~^ ERROR ordinal value in `link_ordinal` is too large: `18446744073709551616`
fn foo();
}

fn main() {}
18 changes: 18 additions & 0 deletions src/test/ui/rfc-2627-raw-dylib/link-ordinal-too-large.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
warning: the feature `raw_dylib` is incomplete and may cause the compiler to crash
--> $DIR/link-ordinal-too-large.rs:1:12
|
LL | #![feature(raw_dylib)]
| ^^^^^^^^^
|
= note: `#[warn(incomplete_features)]` on by default

error: ordinal value in `link_ordinal` is too large: `18446744073709551616`
--> $DIR/link-ordinal-too-large.rs:6:5
|
LL | #[link_ordinal(18446744073709551616)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: the value may not exceed `std::usize::MAX`

error: aborting due to previous error

0 comments on commit 9846af8

Please sign in to comment.