Skip to content

Commit

Permalink
rustdoc: Have no_run imply no_trans
Browse files Browse the repository at this point in the history
This allows writing code examples which pass all analysis of the compiler, but
don't actually link. A good example is examples that use extern {} blocks.

Closes #12903
  • Loading branch information
alexcrichton committed May 3, 2014
1 parent 52955dd commit 9ac9245
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
32 changes: 11 additions & 21 deletions src/doc/guide-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ snappy includes a C interface (documented in
The following is a minimal example of calling a foreign function which will
compile if snappy is installed:

~~~~
~~~~no_run
extern crate libc;
use libc::size_t;
#[link(name = "snappy")]
# #[cfg(ignore_this)]
extern {
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
}
# unsafe fn snappy_max_compressed_length(a: size_t) -> size_t { a }
fn main() {
let x = unsafe { snappy_max_compressed_length(100) };
Expand All @@ -46,7 +44,7 @@ keeping the binding correct at runtime.

The `extern` block can be extended to cover the entire snappy API:

~~~~ {.ignore}
~~~~no_run
extern crate libc;
use libc::{c_int, size_t};
Expand All @@ -67,6 +65,7 @@ extern {
fn snappy_validate_compressed_buffer(compressed: *u8,
compressed_length: size_t) -> c_int;
}
# fn main() {}
~~~~

# Creating a safe interface
Expand Down Expand Up @@ -209,19 +208,16 @@ A basic example is:

Rust code:

~~~~
~~~~no_run
extern fn callback(a:i32) {
println!("I'm called from C with value {0}", a);
}
#[link(name = "extlib")]
# #[cfg(ignore)]
extern {
fn register_callback(cb: extern fn(i32)) -> i32;
fn trigger_callback();
}
# unsafe fn register_callback(cb: extern fn(i32)) -> i32 { 0 }
# unsafe fn trigger_callback() { }
fn main() {
unsafe {
Expand Down Expand Up @@ -265,7 +261,7 @@ referenced Rust object.

Rust code:

~~~~
~~~~no_run
struct RustObject {
a: i32,
Expand All @@ -281,15 +277,11 @@ extern fn callback(target: *mut RustObject, a:i32) {
}
#[link(name = "extlib")]
# #[cfg(ignore)]
extern {
fn register_callback(target: *mut RustObject,
cb: extern fn(*mut RustObject, i32)) -> i32;
fn trigger_callback();
}
# unsafe fn register_callback(a: *mut RustObject,
# b: extern fn(*mut RustObject, i32)) -> i32 { 0 }
# unsafe fn trigger_callback() {}
fn main() {
// Create the object that will be referenced in the callback
Expand Down Expand Up @@ -398,9 +390,12 @@ the `link_args` attribute. This attribute is applied to `extern` blocks and
specifies raw flags which need to get passed to the linker when producing an
artifact. An example usage would be:

~~~ {.ignore}
~~~ no_run
#![feature(link_args)]
#[link_args = "-foo -bar -baz"]
extern {}
# fn main() {}
~~~

Note that this feature is currently hidden behind the `feature(link_args)` gate
Expand Down Expand Up @@ -434,15 +429,13 @@ Foreign APIs often export a global variable which could do something like track
global state. In order to access these variables, you declare them in `extern`
blocks with the `static` keyword:

~~~
~~~no_run
extern crate libc;
#[link(name = "readline")]
# #[cfg(ignore)]
extern {
static rl_readline_version: libc::c_int;
}
# static rl_readline_version: libc::c_int = 0;
fn main() {
println!("You have readline version {} installed.",
Expand All @@ -454,16 +447,14 @@ Alternatively, you may need to alter global state provided by a foreign
interface. To do this, statics can be declared with `mut` so rust can mutate
them.

~~~
~~~no_run
extern crate libc;
use std::ptr;
#[link(name = "readline")]
# #[cfg(ignore)]
extern {
static mut rl_prompt: *libc::c_char;
}
# static mut rl_prompt: *libc::c_char = 0 as *libc::c_char;
fn main() {
"[my-awesome-shell] $".with_c_str(|buf| {
Expand All @@ -488,7 +479,6 @@ extern crate libc;
extern "stdcall" {
fn SetEnvironmentVariableA(n: *u8, v: *u8) -> libc::c_int;
}
# fn main() { }
~~~~

Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
addl_lib_search_paths: RefCell::new(libs),
crate_types: vec!(session::CrateTypeExecutable),
output_types: vec!(link::OutputTypeExe),
no_trans: no_run,
cg: session::CodegenOptions {
prefer_dynamic: true,
.. session::basic_codegen_options()
Expand Down

0 comments on commit 9ac9245

Please sign in to comment.