@@ -493,3 +493,61 @@ operations within documentation test examples, such as `std::fs::read_to_string`
493
493
The ` --test-run-directory ` flag allows controlling the run directory separately from the compilation directory.
494
494
This is particularly useful in workspaces, where compiler invocations and thus diagnostics should be
495
495
relative to the workspace directory, but documentation test examples should run relative to the crate directory.
496
+
497
+ ## Passing arguments to rustc when compiling doctests
498
+
499
+ You can set the ` RUSTFLAGS ` environment variable if you want to add options when compiling the
500
+ doctest. For example if you have:
501
+
502
+ ``` rust,no_run
503
+ /// ```
504
+ /// #![deny(warnings)]
505
+ /// #![feature(async_await)]
506
+ ///
507
+ /// let x = 12;
508
+ /// ```
509
+ pub struct Bar;
510
+ ```
511
+
512
+ And you run ` rustdoc --test ` on it, you will get:
513
+
514
+ ``` console
515
+ running 1 test
516
+ test foo.rs - Bar (line 1) ... FAILED
517
+
518
+ failures:
519
+
520
+ ---- foo.rs - Bar (line 1) stdout ----
521
+ error: the feature `async_await` has been stable since 1.39.0 and no longer requires an attribute to enable
522
+ --> foo.rs:2:12
523
+ |
524
+ 3 | #![feature(async_await)]
525
+ | ^^^^^^^^^^^
526
+ |
527
+ note: the lint level is defined here
528
+ --> foo.rs:1:9
529
+ |
530
+ 2 | #![deny(warnings)]
531
+ | ^^^^^^^^
532
+ = note: `#[deny(stable_features)]` implied by `#[deny(warnings)]`
533
+
534
+ error: aborting due to 1 previous error
535
+
536
+ Couldn't compile the test.
537
+
538
+ failures:
539
+ foo.rs - Bar (line 1)
540
+
541
+ test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.03s
542
+ ```
543
+
544
+ But if you can limit the lint level to warning by using ` RUSTFLAGS=--cap-lints=warn ` :
545
+
546
+ ``` console
547
+ $ RUSTFLAGS=--cap-lints=warn rustdoc --test file.rs
548
+
549
+ running 1 test
550
+ test tests/rustdoc-ui/doctest/rustflags.rs - Bar (line 5) ... ok
551
+
552
+ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.06s
553
+ ```
0 commit comments