Skip to content

Commit f52c318

Browse files
committed
Auto merge of rust-lang#93738 - m-ou-se:rollup-zjyd2et, r=m-ou-se
Rollup of 13 pull requests Successful merges: - rust-lang#88313 (Make the pre-commit script pre-push instead) - rust-lang#91530 (Suggest 1-tuple parentheses on exprs without existing parens) - rust-lang#92724 (Cleanup c_str.rs) - rust-lang#93208 (Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.60.0) - rust-lang#93394 (Don't allow {} to refer to implicit captures in format_args.) - rust-lang#93416 (remove `allow_fail` test flag) - rust-lang#93487 (Fix linking stage1 toolchain in `./x.py setup`) - rust-lang#93673 (Linkify sidebar headings for sibling items) - rust-lang#93680 (Drop json::from_reader) - rust-lang#93682 (Update tracking issue for `const_fn_trait_bound`) - rust-lang#93722 (Use shallow clones for submodules managed by rustbuild, not just bootstrap.py) - rust-lang#93723 (Rerun bootstrap's build script when RUSTC changes) - rust-lang#93737 (bootstrap: prefer using '--config' over 'RUST_BOOTSTRAP_CONFIG') Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents c5e4148 + a6c4810 commit f52c318

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+470
-389
lines changed

compiler/rustc_builtin_macros/src/format.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum ArgumentType {
2525

2626
enum Position {
2727
Exact(usize),
28+
Capture(usize),
2829
Named(Symbol),
2930
}
3031

@@ -49,6 +50,8 @@ struct Context<'a, 'b> {
4950
/// * `arg_unique_types` (in simplified JSON): `[["o", "x"], ["o", "x"], ["o", "x"]]`
5051
/// * `names` (in JSON): `{"foo": 2}`
5152
args: Vec<P<ast::Expr>>,
53+
/// The number of arguments that were added by implicit capturing.
54+
num_captured_args: usize,
5255
/// Placeholder slot numbers indexed by argument.
5356
arg_types: Vec<Vec<usize>>,
5457
/// Unique format specs seen for each argument.
@@ -231,6 +234,11 @@ fn parse_args<'a>(
231234
}
232235

233236
impl<'a, 'b> Context<'a, 'b> {
237+
/// The number of arguments that were explicitly given.
238+
fn num_args(&self) -> usize {
239+
self.args.len() - self.num_captured_args
240+
}
241+
234242
fn resolve_name_inplace(&self, p: &mut parse::Piece<'_>) {
235243
// NOTE: the `unwrap_or` branch is needed in case of invalid format
236244
// arguments, e.g., `format_args!("{foo}")`.
@@ -345,7 +353,7 @@ impl<'a, 'b> Context<'a, 'b> {
345353
}
346354

347355
fn describe_num_args(&self) -> Cow<'_, str> {
348-
match self.args.len() {
356+
match self.num_args() {
349357
0 => "no arguments were given".into(),
350358
1 => "there is 1 argument".into(),
351359
x => format!("there are {} arguments", x).into(),
@@ -371,7 +379,7 @@ impl<'a, 'b> Context<'a, 'b> {
371379

372380
let count = self.pieces.len()
373381
+ self.arg_with_formatting.iter().filter(|fmt| fmt.precision_span.is_some()).count();
374-
if self.names.is_empty() && !numbered_position_args && count != self.args.len() {
382+
if self.names.is_empty() && !numbered_position_args && count != self.num_args() {
375383
e = self.ecx.struct_span_err(
376384
sp,
377385
&format!(
@@ -419,7 +427,7 @@ impl<'a, 'b> Context<'a, 'b> {
419427
if let Some(span) = fmt.precision_span {
420428
let span = self.fmtsp.from_inner(span);
421429
match fmt.precision {
422-
parse::CountIsParam(pos) if pos > self.args.len() => {
430+
parse::CountIsParam(pos) if pos > self.num_args() => {
423431
e.span_label(
424432
span,
425433
&format!(
@@ -462,7 +470,7 @@ impl<'a, 'b> Context<'a, 'b> {
462470
if let Some(span) = fmt.width_span {
463471
let span = self.fmtsp.from_inner(span);
464472
match fmt.width {
465-
parse::CountIsParam(pos) if pos > self.args.len() => {
473+
parse::CountIsParam(pos) if pos > self.num_args() => {
466474
e.span_label(
467475
span,
468476
&format!(
@@ -494,12 +502,15 @@ impl<'a, 'b> Context<'a, 'b> {
494502
/// Actually verifies and tracks a given format placeholder
495503
/// (a.k.a. argument).
496504
fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
505+
if let Exact(arg) = arg {
506+
if arg >= self.num_args() {
507+
self.invalid_refs.push((arg, self.curpiece));
508+
return;
509+
}
510+
}
511+
497512
match arg {
498-
Exact(arg) => {
499-
if self.args.len() <= arg {
500-
self.invalid_refs.push((arg, self.curpiece));
501-
return;
502-
}
513+
Exact(arg) | Capture(arg) => {
503514
match ty {
504515
Placeholder(_) => {
505516
// record every (position, type) combination only once
@@ -526,7 +537,7 @@ impl<'a, 'b> Context<'a, 'b> {
526537
match self.names.get(&name) {
527538
Some(&idx) => {
528539
// Treat as positional arg.
529-
self.verify_arg_type(Exact(idx), ty)
540+
self.verify_arg_type(Capture(idx), ty)
530541
}
531542
None => {
532543
// For the moment capturing variables from format strings expanded from macros is
@@ -541,9 +552,10 @@ impl<'a, 'b> Context<'a, 'b> {
541552
} else {
542553
self.fmtsp
543554
};
555+
self.num_captured_args += 1;
544556
self.args.push(self.ecx.expr_ident(span, Ident::new(name, span)));
545557
self.names.insert(name, idx);
546-
self.verify_arg_type(Exact(idx), ty)
558+
self.verify_arg_type(Capture(idx), ty)
547559
} else {
548560
let msg = format!("there is no argument named `{}`", name);
549561
let sp = if self.is_literal {
@@ -1051,6 +1063,7 @@ pub fn expand_preparsed_format_args(
10511063
let mut cx = Context {
10521064
ecx,
10531065
args,
1066+
num_captured_args: 0,
10541067
arg_types,
10551068
arg_unique_types,
10561069
names,

compiler/rustc_builtin_macros/src/test.rs

-9
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,6 @@ pub fn expand_test_or_bench(
252252
"ignore",
253253
cx.expr_bool(sp, should_ignore(&cx.sess, &item)),
254254
),
255-
// allow_fail: true | false
256-
field(
257-
"allow_fail",
258-
cx.expr_bool(sp, should_fail(&cx.sess, &item)),
259-
),
260255
// compile_fail: true | false
261256
field("compile_fail", cx.expr_bool(sp, false)),
262257
// no_run: true | false
@@ -359,10 +354,6 @@ fn should_ignore(sess: &Session, i: &ast::Item) -> bool {
359354
sess.contains_name(&i.attrs, sym::ignore)
360355
}
361356

362-
fn should_fail(sess: &Session, i: &ast::Item) -> bool {
363-
sess.contains_name(&i.attrs, sym::allow_fail)
364-
}
365-
366357
fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
367358
match cx.sess.find_by_name(&i.attrs, sym::should_panic) {
368359
Some(attr) => {

compiler/rustc_feature/src/active.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,6 @@ declare_features! (
277277
(incomplete, adt_const_params, "1.56.0", Some(44580), None),
278278
/// Allows defining an `#[alloc_error_handler]`.
279279
(active, alloc_error_handler, "1.29.0", Some(51540), None),
280-
/// Allows a test to fail without failing the whole suite.
281-
(active, allow_fail, "1.19.0", Some(46488), None),
282280
/// Allows explicit discriminants on non-unit enum variants.
283281
(active, arbitrary_enum_discriminant, "1.37.0", Some(60553), None),
284282
/// Allows trait methods with arbitrary self types.
@@ -332,7 +330,7 @@ declare_features! (
332330
/// Allows using and casting function pointers in a `const fn`.
333331
(active, const_fn_fn_ptr_basics, "1.48.0", Some(57563), None),
334332
/// Allows trait bounds in `const fn`.
335-
(active, const_fn_trait_bound, "1.53.0", Some(57563), None),
333+
(active, const_fn_trait_bound, "1.53.0", Some(93706), None),
336334
/// Allows `for _ in _` loops in const contexts.
337335
(active, const_for, "1.56.0", Some(87575), None),
338336
/// Allows argument and return position `impl Trait` in a `const fn`.

compiler/rustc_feature/src/builtin_attrs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
403403
},
404404

405405
// Testing:
406-
gated!(allow_fail, Normal, template!(Word), WarnFollowing, experimental!(allow_fail)),
407406
gated!(
408407
test_runner, CrateLevel, template!(List: "path"), ErrorFollowing, custom_test_frameworks,
409408
"custom test frameworks are an unstable feature",

compiler/rustc_feature/src/removed.rs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ declare_features! (
4848
(removed, advanced_slice_patterns, "1.0.0", Some(62254), None,
4949
Some("merged into `#![feature(slice_patterns)]`")),
5050
(removed, allocator, "1.0.0", None, None, None),
51+
/// Allows a test to fail without failing the whole suite.
52+
(removed, allow_fail, "1.19.0", Some(46488), None, Some("removed due to no clear use cases")),
5153
(removed, await_macro, "1.38.0", Some(50547), None,
5254
Some("subsumed by `.await` syntax")),
5355
/// Allows comparing raw pointers during const eval.

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+37-13
Original file line numberDiff line numberDiff line change
@@ -2044,19 +2044,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20442044
// If a tuple of length one was expected and the found expression has
20452045
// parentheses around it, perhaps the user meant to write `(expr,)` to
20462046
// build a tuple (issue #86100)
2047-
(ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => {
2048-
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
2049-
if let Some(code) =
2050-
code.strip_prefix('(').and_then(|s| s.strip_suffix(')'))
2051-
{
2052-
err.span_suggestion(
2053-
span,
2054-
"use a trailing comma to create a tuple with one element",
2055-
format!("({},)", code),
2056-
Applicability::MaybeIncorrect,
2057-
);
2058-
}
2059-
}
2047+
(ty::Tuple(_), _) => {
2048+
self.emit_tuple_wrap_err(&mut err, span, found, expected)
20602049
}
20612050
// If a character was expected and the found expression is a string literal
20622051
// containing a single character, perhaps the user meant to write `'c'` to
@@ -2119,6 +2108,41 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21192108
diag
21202109
}
21212110

2111+
fn emit_tuple_wrap_err(
2112+
&self,
2113+
err: &mut DiagnosticBuilder<'tcx>,
2114+
span: Span,
2115+
found: Ty<'tcx>,
2116+
expected: Ty<'tcx>,
2117+
) {
2118+
let [expected_tup_elem] = &expected.tuple_fields().collect::<Vec<_>>()[..]
2119+
else { return };
2120+
2121+
if !same_type_modulo_infer(expected_tup_elem, found) {
2122+
return;
2123+
}
2124+
2125+
let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
2126+
else { return };
2127+
2128+
let msg = "use a trailing comma to create a tuple with one element";
2129+
if code.starts_with('(') && code.ends_with(')') {
2130+
let before_close = span.hi() - BytePos::from_u32(1);
2131+
err.span_suggestion(
2132+
span.with_hi(before_close).shrink_to_hi(),
2133+
msg,
2134+
",".into(),
2135+
Applicability::MachineApplicable,
2136+
);
2137+
} else {
2138+
err.multipart_suggestion(
2139+
msg,
2140+
vec![(span.shrink_to_lo(), "(".into()), (span.shrink_to_hi(), ",)".into())],
2141+
Applicability::MachineApplicable,
2142+
);
2143+
}
2144+
}
2145+
21222146
fn values_str(
21232147
&self,
21242148
values: ValuePairs<'tcx>,

compiler/rustc_serialize/src/json.rs

-22
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ use self::ParserState::*;
185185

186186
use std::borrow::Cow;
187187
use std::collections::{BTreeMap, HashMap};
188-
use std::io;
189-
use std::io::prelude::*;
190188
use std::mem::swap;
191189
use std::num::FpCategory as Fp;
192190
use std::ops::Index;
@@ -250,7 +248,6 @@ pub enum ErrorCode {
250248
pub enum ParserError {
251249
/// msg, line, col
252250
SyntaxError(ErrorCode, usize, usize),
253-
IoError(io::ErrorKind, String),
254251
}
255252

256253
// Builder and Parser have the same errors.
@@ -329,10 +326,6 @@ impl fmt::Display for ErrorCode {
329326
}
330327
}
331328

332-
fn io_error_to_error(io: io::Error) -> ParserError {
333-
IoError(io.kind(), io.to_string())
334-
}
335-
336329
impl fmt::Display for ParserError {
337330
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
338331
// FIXME this should be a nicer error
@@ -2163,21 +2156,6 @@ impl<T: Iterator<Item = char>> Builder<T> {
21632156
}
21642157
}
21652158

2166-
/// Decodes a json value from an `&mut io::Read`
2167-
pub fn from_reader(rdr: &mut dyn Read) -> Result<Json, BuilderError> {
2168-
let mut contents = Vec::new();
2169-
match rdr.read_to_end(&mut contents) {
2170-
Ok(c) => c,
2171-
Err(e) => return Err(io_error_to_error(e)),
2172-
};
2173-
let s = match str::from_utf8(&contents).ok() {
2174-
Some(s) => s,
2175-
_ => return Err(SyntaxError(NotUtf8, 0, 0)),
2176-
};
2177-
let mut builder = Builder::new(s.chars());
2178-
builder.build()
2179-
}
2180-
21812159
/// Decodes a json value from a string
21822160
pub fn from_str(s: &str) -> Result<Json, BuilderError> {
21832161
let mut builder = Builder::new(s.chars());

compiler/rustc_target/src/spec/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2148,8 +2148,8 @@ impl Target {
21482148
use std::fs;
21492149

21502150
fn load_file(path: &Path) -> Result<(Target, TargetWarnings), String> {
2151-
let contents = fs::read(path).map_err(|e| e.to_string())?;
2152-
let obj = json::from_reader(&mut &contents[..]).map_err(|e| e.to_string())?;
2151+
let contents = fs::read_to_string(path).map_err(|e| e.to_string())?;
2152+
let obj = json::from_str(&contents).map_err(|e| e.to_string())?;
21532153
Target::from_json(obj)
21542154
}
21552155

0 commit comments

Comments
 (0)