Skip to content

Commit

Permalink
Auto merge of rust-lang#131859 - chriskrycho:update-trpl, r=onur-ozkan
Browse files Browse the repository at this point in the history
Update TRPL to add new Chapter 17: Async and Await

- Add support to `rustbook` to pass through the `-L`/`--library-path` flag to `mdbook` so that references to the `trpl` crate
- Build the `trpl` crate as part of the book tests. Make it straightforward to add other such book dependencies in the future if needed by implementing that in a fairly general way.
- Update the submodule for the book to pull in the new chapter on async and await, as well as a number of other fixes. This will happen organically/automatically in a week, too, but this lets me group this change with the next one:
- Update the compiler messages which reference the existing chapters 17–20, which are now chapters 18-21. There are only two, both previously referencing chapter 18.
- Update the UI tests which reference the compiler message outputs.
  • Loading branch information
bors authored and compiler-errors committed Nov 24, 2024
2 parents 15b663e + 7630b49 commit ae1c8ea
Show file tree
Hide file tree
Showing 82 changed files with 297 additions and 219 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn report_unexpected_variant_res(
.with_code(err_code);
match res {
Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == E0164 => {
let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html";
let patterns_url = "https://doc.rust-lang.org/book/ch19-00-patterns.html";
err.with_span_label(span, "`fn` calls are not allowed in patterns")
.with_help(format!("for more information, visit {patterns_url}"))
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ impl<'tcx> Const<'tcx> {
}

impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
fn try_to_target_usize(self, interner: TyCtxt<'tcx>) -> Option<u64> {
self.try_to_target_usize(interner)
}

fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst) -> Self {
Const::new_infer(tcx, infer)
}
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_middle/src/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ impl<'tcx> TypeError<'tcx> {
pluralize!(values.found)
)
.into(),
TypeError::FixedArraySize(values) => format!(
"expected an array with a fixed size of {} element{}, found one with {} element{}",
values.expected,
pluralize!(values.expected),
values.found,
pluralize!(values.found)
TypeError::ArraySize(values) => format!(
"expected an array with a size of {}, found one with a size of {}",
values.expected, values.found,
)
.into(),
TypeError::ArgCount => "incorrect number of function parameters".into(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ mir_build_lower_range_bound_must_be_less_than_or_equal_to_upper =
mir_build_lower_range_bound_must_be_less_than_upper = lower range bound must be less than upper
mir_build_more_information = for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
mir_build_more_information = for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html
mir_build_moved = value is moved into `{$name}` here
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ parse_unexpected_expr_in_pat =
}, found an expression
.label = not a pattern
.note = arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>
.note = arbitrary expressions are not allowed in patterns: <https://doc.rust-lang.org/book/ch19-00-patterns.html>
parse_unexpected_expr_in_pat_const_sugg = consider extracting the expression into a `const`
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
let PathSource::TupleStruct(_, _) = source else { return };
let Some(Res::Def(DefKind::Fn, _)) = res else { return };
err.primary_message("expected a pattern, found a function call");
err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch18-00-patterns.html>");
err.note("function calls are not allowed in patterns: <https://doc.rust-lang.org/book/ch19-00-patterns.html>");
}

fn suggest_changing_type_to_const_param(
Expand Down
13 changes: 9 additions & 4 deletions compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,12 +1792,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {

fn suggest_specify_actual_length(
&self,
terr: TypeError<'_>,
trace: &TypeTrace<'_>,
terr: TypeError<'tcx>,
trace: &TypeTrace<'tcx>,
span: Span,
) -> Option<TypeErrorAdditionalDiags> {
let hir = self.tcx.hir();
let TypeError::FixedArraySize(sz) = terr else {
let TypeError::ArraySize(sz) = terr else {
return None;
};
let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
Expand Down Expand Up @@ -1838,9 +1838,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if let Some(tykind) = tykind
&& let hir::TyKind::Array(_, length) = tykind
&& let hir::ArrayLen::Body(ct) = length
&& let Some((scalar, ty)) = sz.found.try_to_scalar()
&& ty == self.tcx.types.usize
{
let span = ct.span();
Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength { span, length: sz.found })
Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength {
span,
length: scalar.to_target_usize(&self.tcx).unwrap(),
})
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_type_ir/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub enum TypeError<I: Interner> {
Mutability,
ArgumentMutability(usize),
TupleSize(ExpectedFound<usize>),
FixedArraySize(ExpectedFound<u64>),
ArraySize(ExpectedFound<I::Const>),
ArgCount,

RegionsDoesNotOutlive(I::Region, I::Region),
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<I: Interner> TypeError<I> {
use self::TypeError::*;
match self {
CyclicTy(_) | CyclicConst(_) | SafetyMismatch(_) | PolarityMismatch(_) | Mismatch
| AbiMismatch(_) | FixedArraySize(_) | ArgumentSorts(..) | Sorts(_)
| AbiMismatch(_) | ArraySize(_) | ArgumentSorts(..) | Sorts(_)
| VariadicMismatch(_) | TargetFeatureCast(_) => false,

Mutability
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_type_ir/src/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ pub trait Const<I: Interner<Const = Self>>:
+ Relate<I>
+ Flags
{
fn try_to_target_usize(self, interner: I) -> Option<u64>;

fn new_infer(interner: I, var: ty::InferConst) -> Self;

fn new_var(interner: I, var: ty::ConstVid) -> Self;
Expand Down
15 changes: 3 additions & 12 deletions compiler/rustc_type_ir/src/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,19 +501,10 @@ pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
let t = relation.relate(a_t, b_t)?;
match relation.relate(sz_a, sz_b) {
Ok(sz) => Ok(Ty::new_array_with_const_len(cx, t, sz)),
Err(err) => {
// Check whether the lengths are both concrete/known values,
// but are unequal, for better diagnostics.
let sz_a = sz_a.try_to_target_usize(cx);
let sz_b = sz_b.try_to_target_usize(cx);

match (sz_a, sz_b) {
(Some(sz_a_val), Some(sz_b_val)) if sz_a_val != sz_b_val => {
Err(TypeError::FixedArraySize(ExpectedFound::new(sz_a_val, sz_b_val)))
}
_ => Err(err),
}
Err(TypeError::ConstMismatch(_)) => {
Err(TypeError::ArraySize(ExpectedFound::new(sz_a, sz_b)))
}
Err(e) => Err(e),
}
}

Expand Down
67 changes: 66 additions & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
//! `./x.py test` (aka [`Kind::Test`]) is currently allowed to reach build steps in other modules.
//! However, this contains ~all test parts we expect people to be able to build and run locally.
use std::collections::HashSet;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::{env, fs, iter};

use clap_complete::shells;

use crate::core::build_steps::compile::run_cargo;
use crate::core::build_steps::doc::DocumentationFormat;
use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
use crate::core::build_steps::tool::{self, SourceType, Tool};
Expand Down Expand Up @@ -2185,6 +2187,7 @@ struct BookTest {
path: PathBuf,
name: &'static str,
is_ext_doc: bool,
dependencies: Vec<&'static str>,
}

impl Step for BookTest {
Expand Down Expand Up @@ -2237,6 +2240,57 @@ impl BookTest {
// Books often have feature-gated example text.
rustbook_cmd.env("RUSTC_BOOTSTRAP", "1");
rustbook_cmd.env("PATH", new_path).arg("test").arg(path);

// Books may also need to build dependencies. For example, `TheBook` has
// code samples which use the `trpl` crate. For the `rustdoc` invocation
// to find them them successfully, they need to be built first and their
// paths used to generate the
let libs = if !self.dependencies.is_empty() {
let mut lib_paths = vec![];
for dep in self.dependencies {
let mode = Mode::ToolRustc;
let target = builder.config.build;
let cargo = tool::prepare_tool_cargo(
builder,
compiler,
mode,
target,
Kind::Build,
dep,
SourceType::Submodule,
&[],
);

let stamp = builder
.cargo_out(compiler, mode, target)
.join(PathBuf::from(dep).file_name().unwrap())
.with_extension("stamp");

let output_paths = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
let directories = output_paths
.into_iter()
.filter_map(|p| p.parent().map(ToOwned::to_owned))
.fold(HashSet::new(), |mut set, dir| {
set.insert(dir);
set
});

lib_paths.extend(directories);
}
lib_paths
} else {
vec![]
};

if !libs.is_empty() {
let paths = libs
.into_iter()
.map(|path| path.into_os_string())
.collect::<Vec<OsString>>()
.join(OsStr::new(","));
rustbook_cmd.args([OsString::from("--library-path"), paths]);
}

builder.add_rust_test_threads(&mut rustbook_cmd);
let _guard = builder.msg(
Kind::Test,
Expand Down Expand Up @@ -2295,6 +2349,7 @@ macro_rules! test_book {
$name:ident, $path:expr, $book_name:expr,
default=$default:expr
$(,submodules = $submodules:expr)?
$(,dependencies=$dependencies:expr)?
;
)+) => {
$(
Expand Down Expand Up @@ -2324,11 +2379,21 @@ macro_rules! test_book {
builder.require_submodule(submodule, None);
}
)*

let dependencies = vec![];
$(
let mut dependencies = dependencies;
for dep in $dependencies {
dependencies.push(dep);
}
)?

builder.ensure(BookTest {
compiler: self.compiler,
path: PathBuf::from($path),
name: $book_name,
is_ext_doc: !$default,
dependencies,
});
}
}
Expand All @@ -2343,7 +2408,7 @@ test_book!(
RustcBook, "src/doc/rustc", "rustc", default=true;
RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false, submodules=["src/doc/rust-by-example"];
EmbeddedBook, "src/doc/embedded-book", "embedded-book", default=false, submodules=["src/doc/embedded-book"];
TheBook, "src/doc/book", "book", default=false, submodules=["src/doc/book"];
TheBook, "src/doc/book", "book", default=false, submodules=["src/doc/book"], dependencies=["src/doc/book/packages/trpl"];
UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
EditionGuide, "src/doc/edition-guide", "edition-guide", default=false, submodules=["src/doc/edition-guide"];
);
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/src/core/build_steps/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn default_paths_to_vendor(builder: &Builder<'_>) -> Vec<(PathBuf, Vec<&'sta
("src/tools/rustbook/Cargo.toml", SUBMODULES_FOR_RUSTBOOK.into()),
("src/tools/rustc-perf/Cargo.toml", vec!["src/tools/rustc-perf"]),
("src/tools/opt-dist/Cargo.toml", vec![]),
("src/doc/book/packages/trpl/Cargo.toml", vec![]),
]
.into_iter()
.map(|(path, submodules)| (builder.src.join(path), submodules))
Expand Down
2 changes: 1 addition & 1 deletion src/doc/book
Submodule book updated 884 files
2 changes: 1 addition & 1 deletion src/tools/rustbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env_logger = "0.11"
mdbook-trpl-listing = { path = "../../doc/book/packages/mdbook-trpl-listing" }
mdbook-trpl-note = { path = "../../doc/book/packages/mdbook-trpl-note" }
mdbook-i18n-helpers = "0.3.3"
mdbook-spec = { path = "../../doc/reference/mdbook-spec"}
mdbook-spec = { path = "../../doc/reference/mdbook-spec" }

[dependencies.mdbook]
version = "0.4.37"
Expand Down
31 changes: 27 additions & 4 deletions src/tools/rustbook/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ fn main() {
(Defaults to the current directory when omitted)")
.value_parser(clap::value_parser!(PathBuf));

// Note: we don't parse this into a `PathBuf` because it is comma separated
// strings *and* we will ultimately pass it into `MDBook::test()`, which
// accepts `Vec<&str>`. Although it is a bit annoying that `-l/--lang` and
// `-L/--library-path` are so close, this is the same set of arguments we
// would pass when invoking mdbook on the CLI, so making them match when
// invoking rustbook makes for good consistency.
let library_path_arg = arg!(
-L --"library-path" <PATHS>
"A comma-separated list of directories to add to the crate search\n\
path when building tests"
)
.required(false)
.value_parser(parse_library_paths);

let matches = Command::new("rustbook")
.about("Build a book with mdBook")
.author("Steve Klabnik <[email protected]>")
Expand All @@ -48,11 +62,12 @@ fn main() {
.subcommand(
Command::new("test")
.about("Tests that a book's Rust code samples compile")
.arg(dir_arg),
.arg(dir_arg)
.arg(library_path_arg),
)
.get_matches();

// Check which subcomamnd the user ran...
// Check which subcommand the user ran...
match matches.subcommand() {
Some(("build", sub_matches)) => {
if let Err(e) = build(sub_matches) {
Expand Down Expand Up @@ -113,8 +128,12 @@ pub fn build(args: &ArgMatches) -> Result3<()> {

fn test(args: &ArgMatches) -> Result3<()> {
let book_dir = get_book_dir(args);
let library_paths = args
.try_get_one::<Vec<String>>("library-path")?
.map(|v| v.iter().map(|s| s.as_str()).collect::<Vec<&str>>())
.unwrap_or_default();
let mut book = load_book(&book_dir)?;
book.test(vec![])
book.test(library_paths)
}

fn get_book_dir(args: &ArgMatches) -> PathBuf {
Expand All @@ -132,12 +151,16 @@ fn load_book(book_dir: &Path) -> Result3<MDBook> {
Ok(book)
}

fn parse_library_paths(input: &str) -> Result<Vec<String>, String> {
Ok(input.split(",").map(String::from).collect())
}

fn handle_error(error: mdbook::errors::Error) -> ! {
eprintln!("Error: {}", error);

for cause in error.chain().skip(1) {
eprintln!("\tCaused By: {}", cause);
}

::std::process::exit(101);
std::process::exit(101);
}
9 changes: 0 additions & 9 deletions tests/crashes/126359.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/crashes/131101.rs

This file was deleted.

5 changes: 1 addition & 4 deletions tests/ui/array-slice-vec/match_arr_unknown_len.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ error[E0308]: mismatched types
--> $DIR/match_arr_unknown_len.rs:3:9
|
LL | [1, 2] => true,
| ^^^^^^ expected `2`, found `N`
|
= note: expected array `[u32; 2]`
found array `[u32; N]`
| ^^^^^^ expected an array with a size of 2, found one with a size of N

error: aborting due to 1 previous error

Expand Down
Loading

0 comments on commit ae1c8ea

Please sign in to comment.