Skip to content

Commit 9ac9245

Browse files
committed
rustdoc: Have no_run imply no_trans
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
1 parent 52955dd commit 9ac9245

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

src/doc/guide-ffi.md

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ snappy includes a C interface (documented in
1111
The following is a minimal example of calling a foreign function which will
1212
compile if snappy is installed:
1313

14-
~~~~
14+
~~~~no_run
1515
extern crate libc;
1616
use libc::size_t;
1717
1818
#[link(name = "snappy")]
19-
# #[cfg(ignore_this)]
2019
extern {
2120
fn snappy_max_compressed_length(source_length: size_t) -> size_t;
2221
}
23-
# unsafe fn snappy_max_compressed_length(a: size_t) -> size_t { a }
2422
2523
fn main() {
2624
let x = unsafe { snappy_max_compressed_length(100) };
@@ -46,7 +44,7 @@ keeping the binding correct at runtime.
4644

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

49-
~~~~ {.ignore}
47+
~~~~no_run
5048
extern crate libc;
5149
use libc::{c_int, size_t};
5250
@@ -67,6 +65,7 @@ extern {
6765
fn snappy_validate_compressed_buffer(compressed: *u8,
6866
compressed_length: size_t) -> c_int;
6967
}
68+
# fn main() {}
7069
~~~~
7170

7271
# Creating a safe interface
@@ -209,19 +208,16 @@ A basic example is:
209208

210209
Rust code:
211210

212-
~~~~
211+
~~~~no_run
213212
extern fn callback(a:i32) {
214213
println!("I'm called from C with value {0}", a);
215214
}
216215
217216
#[link(name = "extlib")]
218-
# #[cfg(ignore)]
219217
extern {
220218
fn register_callback(cb: extern fn(i32)) -> i32;
221219
fn trigger_callback();
222220
}
223-
# unsafe fn register_callback(cb: extern fn(i32)) -> i32 { 0 }
224-
# unsafe fn trigger_callback() { }
225221
226222
fn main() {
227223
unsafe {
@@ -265,7 +261,7 @@ referenced Rust object.
265261

266262
Rust code:
267263

268-
~~~~
264+
~~~~no_run
269265
270266
struct RustObject {
271267
a: i32,
@@ -281,15 +277,11 @@ extern fn callback(target: *mut RustObject, a:i32) {
281277
}
282278
283279
#[link(name = "extlib")]
284-
# #[cfg(ignore)]
285280
extern {
286281
fn register_callback(target: *mut RustObject,
287282
cb: extern fn(*mut RustObject, i32)) -> i32;
288283
fn trigger_callback();
289284
}
290-
# unsafe fn register_callback(a: *mut RustObject,
291-
# b: extern fn(*mut RustObject, i32)) -> i32 { 0 }
292-
# unsafe fn trigger_callback() {}
293285
294286
fn main() {
295287
// Create the object that will be referenced in the callback
@@ -398,9 +390,12 @@ the `link_args` attribute. This attribute is applied to `extern` blocks and
398390
specifies raw flags which need to get passed to the linker when producing an
399391
artifact. An example usage would be:
400392

401-
~~~ {.ignore}
393+
~~~ no_run
394+
#![feature(link_args)]
395+
402396
#[link_args = "-foo -bar -baz"]
403397
extern {}
398+
# fn main() {}
404399
~~~
405400

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

437-
~~~
432+
~~~no_run
438433
extern crate libc;
439434
440435
#[link(name = "readline")]
441-
# #[cfg(ignore)]
442436
extern {
443437
static rl_readline_version: libc::c_int;
444438
}
445-
# static rl_readline_version: libc::c_int = 0;
446439
447440
fn main() {
448441
println!("You have readline version {} installed.",
@@ -454,16 +447,14 @@ Alternatively, you may need to alter global state provided by a foreign
454447
interface. To do this, statics can be declared with `mut` so rust can mutate
455448
them.
456449

457-
~~~
450+
~~~no_run
458451
extern crate libc;
459452
use std::ptr;
460453
461454
#[link(name = "readline")]
462-
# #[cfg(ignore)]
463455
extern {
464456
static mut rl_prompt: *libc::c_char;
465457
}
466-
# static mut rl_prompt: *libc::c_char = 0 as *libc::c_char;
467458
468459
fn main() {
469460
"[my-awesome-shell] $".with_c_str(|buf| {
@@ -488,7 +479,6 @@ extern crate libc;
488479
extern "stdcall" {
489480
fn SetEnvironmentVariableA(n: *u8, v: *u8) -> libc::c_int;
490481
}
491-
492482
# fn main() { }
493483
~~~~
494484

src/librustdoc/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
103103
addl_lib_search_paths: RefCell::new(libs),
104104
crate_types: vec!(session::CrateTypeExecutable),
105105
output_types: vec!(link::OutputTypeExe),
106+
no_trans: no_run,
106107
cg: session::CodegenOptions {
107108
prefer_dynamic: true,
108109
.. session::basic_codegen_options()

0 commit comments

Comments
 (0)