From 27b9b166d13024ca103dc8d611724b06c32302da Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Tue, 2 Aug 2022 01:08:13 +0100 Subject: [PATCH 01/13] Error on broken pipe but do not ICE --- compiler/rustc_driver/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 53ae913f94f12..94639bf8e1ee4 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -1148,6 +1148,17 @@ static DEFAULT_HOOK: LazyLock) + Sync + Send + LazyLock::new(|| { let hook = panic::take_hook(); panic::set_hook(Box::new(|info| { + // If the error was caused by a broken pipe then this is not a bug. + // Write the error and return immediately. See #98700. + #[cfg(windows)] + if let Some(msg) = info.payload().downcast_ref::() { + if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)") + { + early_error_no_abort(ErrorOutputType::default(), &msg); + return; + } + }; + // Invoke the default handler, which prints the actual panic message and optionally a backtrace (*DEFAULT_HOOK)(info); From 9cf570995cded5e224d5dba8296a85fdbe1c6918 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 3 Aug 2022 06:28:45 +0000 Subject: [PATCH 02/13] Suggest expressions' fields even if they're not ADTs --- compiler/rustc_typeck/src/check/expr.rs | 12 ++++++------ src/test/ui/copy-a-resource.stderr | 4 ++++ src/test/ui/issues/issue-2823.stderr | 4 ++++ src/test/ui/issues/issue-31173.stderr | 4 ++++ src/test/ui/issues/issue-39175.stderr | 4 ++++ src/test/ui/mismatched_types/issue-36053-2.stderr | 4 ++++ src/test/ui/noncopyable-class.stderr | 8 ++++++++ .../suggestions/import-trait-for-method-call.stderr | 4 ++++ .../ui/suggestions/mut-borrow-needed-by-trait.stderr | 10 ++++++++++ src/test/ui/suggestions/suggest-using-chars.stderr | 8 ++++++++ .../ui/union/union-derive-clone.mirunsafeck.stderr | 4 ++++ .../ui/union/union-derive-clone.thirunsafeck.stderr | 4 ++++ src/test/ui/unique-object-noncopyable.stderr | 8 ++++++++ src/test/ui/unique-pinned-nocopy.stderr | 8 ++++++++ 14 files changed, 80 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 6e97b0bf2ab7d..a93588351361e 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -2611,15 +2611,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // up to a depth of three None } else { - // recursively search fields of `candidate_field` if it's a ty::Adt field_path.push(candidate_field.ident(self.tcx).normalize_to_macros_2_0()); let field_ty = candidate_field.ty(self.tcx, subst); - if let Some((nested_fields, subst)) = self.get_field_candidates(span, field_ty) { - for field in nested_fields.iter() { + if matches(candidate_field, field_ty) { + return Some(field_path); + } else if let Some((nested_fields, subst)) = self.get_field_candidates(span, field_ty) { + // recursively search fields of `candidate_field` if it's a ty::Adt + for field in nested_fields { if field.vis.is_accessible_from(id, self.tcx) { - if matches(candidate_field, field_ty) { - return Some(field_path); - } else if let Some(field_path) = self.check_for_nested_field_satisfying( + if let Some(field_path) = self.check_for_nested_field_satisfying( span, matches, field, diff --git a/src/test/ui/copy-a-resource.stderr b/src/test/ui/copy-a-resource.stderr index 128087f1e3755..b92449c6e0aff 100644 --- a/src/test/ui/copy-a-resource.stderr +++ b/src/test/ui/copy-a-resource.stderr @@ -10,6 +10,10 @@ LL | let _y = x.clone(); = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `Clone` +help: one of the expressions' fields has a method of the same name + | +LL | let _y = x.i.clone(); + | ++ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-2823.stderr b/src/test/ui/issues/issue-2823.stderr index b5a2b2f55a6d4..208b340d06410 100644 --- a/src/test/ui/issues/issue-2823.stderr +++ b/src/test/ui/issues/issue-2823.stderr @@ -10,6 +10,10 @@ LL | let _d = c.clone(); = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `Clone` +help: one of the expressions' fields has a method of the same name + | +LL | let _d = c.x.clone(); + | ++ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-31173.stderr b/src/test/ui/issues/issue-31173.stderr index 68337a715e14f..e8797ea7b5b4c 100644 --- a/src/test/ui/issues/issue-31173.stderr +++ b/src/test/ui/issues/issue-31173.stderr @@ -33,6 +33,10 @@ LL | pub struct TakeWhile { which is required by `Cloned, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator` `Cloned, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator` which is required by `&mut Cloned, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator` +help: one of the expressions' fields has a method of the same name + | +LL | .it.collect(); + | +++ error: aborting due to 2 previous errors diff --git a/src/test/ui/issues/issue-39175.stderr b/src/test/ui/issues/issue-39175.stderr index afceae82e68be..b19f58d2a381b 100644 --- a/src/test/ui/issues/issue-39175.stderr +++ b/src/test/ui/issues/issue-39175.stderr @@ -5,6 +5,10 @@ LL | Command::new("echo").arg("hello").exec(); | ^^^^ method not found in `&mut Command` | = help: items from traits can only be used if the trait is in scope +help: one of the expressions' fields has a method of the same name + | +LL | Command::new("echo").arg("hello").inner.exec(); + | ++++++ help: the following trait is implemented but not in scope; perhaps add a `use` for it: | LL | use std::os::unix::process::CommandExt; diff --git a/src/test/ui/mismatched_types/issue-36053-2.stderr b/src/test/ui/mismatched_types/issue-36053-2.stderr index b11ea97d160be..c3c8e5f272e3f 100644 --- a/src/test/ui/mismatched_types/issue-36053-2.stderr +++ b/src/test/ui/mismatched_types/issue-36053-2.stderr @@ -35,6 +35,10 @@ LL | pub struct Filter { which is required by `Filter>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator` `Filter>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator` which is required by `&mut Filter>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator` +help: one of the expressions' fields has a method of the same name + | +LL | once::<&str>("str").fuse().filter(|a: &str| true).iter.count(); + | +++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/noncopyable-class.stderr b/src/test/ui/noncopyable-class.stderr index 0c696163a26c5..15e22e946da8a 100644 --- a/src/test/ui/noncopyable-class.stderr +++ b/src/test/ui/noncopyable-class.stderr @@ -10,6 +10,14 @@ LL | let _y = x.clone(); = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `Clone` +help: one of the expressions' fields has a method of the same name + | +LL | let _y = x.i.clone(); + | ++ +help: one of the expressions' fields has a method of the same name + | +LL | let _y = x.j.x.clone(); + | ++++ error: aborting due to previous error diff --git a/src/test/ui/suggestions/import-trait-for-method-call.stderr b/src/test/ui/suggestions/import-trait-for-method-call.stderr index bac8de7987256..f220458f321f0 100644 --- a/src/test/ui/suggestions/import-trait-for-method-call.stderr +++ b/src/test/ui/suggestions/import-trait-for-method-call.stderr @@ -10,6 +10,10 @@ LL | fn finish(&self) -> u64; | ------ the method is available for `DefaultHasher` here | = help: items from traits can only be used if the trait is in scope +help: one of the expressions' fields has a method of the same name + | +LL | h.0.finish() + | ++ help: the following trait is implemented but not in scope; perhaps add a `use` for it: | LL | use std::hash::Hasher; diff --git a/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr b/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr index d121932c842e3..e19bc5a1fd48d 100644 --- a/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr +++ b/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr @@ -41,6 +41,16 @@ LL | pub struct BufWriter { `&dyn std::io::Write: std::io::Write` which is required by `BufWriter<&dyn std::io::Write>: std::io::Write` = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) +help: one of the expressions' fields has a method of the same name + --> $SRC_DIR/core/src/macros/mod.rs:LL:COL + | +LL | $dst.inner.write_fmt($crate::format_args_nl!($($arg)*)) + | ++++++ +help: one of the expressions' fields has a method of the same name + --> $SRC_DIR/core/src/macros/mod.rs:LL:COL + | +LL | $dst.buf.write_fmt($crate::format_args_nl!($($arg)*)) + | ++++ error: aborting due to 3 previous errors diff --git a/src/test/ui/suggestions/suggest-using-chars.stderr b/src/test/ui/suggestions/suggest-using-chars.stderr index 99bcfb08a0892..1690309719fa7 100644 --- a/src/test/ui/suggestions/suggest-using-chars.stderr +++ b/src/test/ui/suggestions/suggest-using-chars.stderr @@ -25,6 +25,10 @@ help: because of the in-memory representation of `&str`, to obtain an `Iterator` | LL | let _ = String::from("bar").chars(); | ~~~~~ +help: one of the expressions' fields has a method of the same name + | +LL | let _ = String::from("bar").vec.iter(); + | ++++ error[E0599]: no method named `iter` found for reference `&String` in the current scope --> $DIR/suggest-using-chars.rs:5:36 @@ -36,6 +40,10 @@ help: because of the in-memory representation of `&str`, to obtain an `Iterator` | LL | let _ = (&String::from("bar")).chars(); | ~~~~~ +help: one of the expressions' fields has a method of the same name + | +LL | let _ = (&String::from("bar")).vec.iter(); + | ++++ error[E0599]: no method named `iter` found for type `{integer}` in the current scope --> $DIR/suggest-using-chars.rs:6:15 diff --git a/src/test/ui/union/union-derive-clone.mirunsafeck.stderr b/src/test/ui/union/union-derive-clone.mirunsafeck.stderr index 148fb5046705b..44c9d4a84387e 100644 --- a/src/test/ui/union/union-derive-clone.mirunsafeck.stderr +++ b/src/test/ui/union/union-derive-clone.mirunsafeck.stderr @@ -42,6 +42,10 @@ help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]` | LL | #[derive(Clone, Copy)] | +help: one of the expressions' fields has a method of the same name + | +LL | let w = u.a.clone(); + | ++ error: aborting due to 2 previous errors diff --git a/src/test/ui/union/union-derive-clone.thirunsafeck.stderr b/src/test/ui/union/union-derive-clone.thirunsafeck.stderr index 148fb5046705b..44c9d4a84387e 100644 --- a/src/test/ui/union/union-derive-clone.thirunsafeck.stderr +++ b/src/test/ui/union/union-derive-clone.thirunsafeck.stderr @@ -42,6 +42,10 @@ help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]` | LL | #[derive(Clone, Copy)] | +help: one of the expressions' fields has a method of the same name + | +LL | let w = u.a.clone(); + | ++ error: aborting due to 2 previous errors diff --git a/src/test/ui/unique-object-noncopyable.stderr b/src/test/ui/unique-object-noncopyable.stderr index 98a9bd07ed21d..12917d54114e9 100644 --- a/src/test/ui/unique-object-noncopyable.stderr +++ b/src/test/ui/unique-object-noncopyable.stderr @@ -23,6 +23,14 @@ LL | | >(Unique, A); which is required by `Box: Clone` `dyn Foo: Clone` which is required by `Box: Clone` +help: one of the expressions' fields has a method of the same name + | +LL | let _z = y.0.clone(); + | ++ +help: one of the expressions' fields has a method of the same name + | +LL | let _z = y.1.clone(); + | ++ error: aborting due to previous error diff --git a/src/test/ui/unique-pinned-nocopy.stderr b/src/test/ui/unique-pinned-nocopy.stderr index 7af9c684b72e3..cc9bdd26e11c5 100644 --- a/src/test/ui/unique-pinned-nocopy.stderr +++ b/src/test/ui/unique-pinned-nocopy.stderr @@ -25,6 +25,14 @@ help: consider annotating `R` with `#[derive(Clone)]` | LL | #[derive(Clone)] | +help: one of the expressions' fields has a method of the same name + | +LL | let _j = i.0.clone(); + | ++ +help: one of the expressions' fields has a method of the same name + | +LL | let _j = i.1.clone(); + | ++ error: aborting due to previous error From 4df6cbe96fe3e356aff89155f58a497d48bc78ee Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 3 Aug 2022 06:42:19 +0000 Subject: [PATCH 03/13] Consider privacy more carefully when suggesting accessing fields --- compiler/rustc_typeck/src/check/expr.rs | 63 +++++++++++-------- .../rustc_typeck/src/check/method/suggest.rs | 7 ++- src/test/ui/issues/issue-31173.stderr | 4 -- src/test/ui/issues/issue-39175.stderr | 4 -- .../ui/mismatched_types/issue-36053-2.stderr | 4 -- .../import-trait-for-method-call.stderr | 4 -- .../mut-borrow-needed-by-trait.stderr | 10 --- .../ui/suggestions/suggest-using-chars.stderr | 8 --- src/test/ui/unique-object-noncopyable.stderr | 8 --- src/test/ui/unique-pinned-nocopy.stderr | 8 --- 10 files changed, 40 insertions(+), 80 deletions(-) diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index a93588351361e..523a10cc36a9e 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -2535,15 +2535,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); // try to add a suggestion in case the field is a nested field of a field of the Adt - if let Some((fields, substs)) = self.get_field_candidates(span, expr_t) { - for candidate_field in fields.iter() { + let mod_id = self.tcx.parent_module(id).to_def_id(); + if let Some((fields, substs)) = + self.get_field_candidates_considering_privacy(span, expr_t, mod_id) + { + for candidate_field in fields { if let Some(mut field_path) = self.check_for_nested_field_satisfying( span, &|candidate_field, _| candidate_field.ident(self.tcx()) == field, candidate_field, substs, vec![], - self.tcx.parent_module(id).to_def_id(), + mod_id, ) { // field_path includes `field` that we're looking for, so pop it. field_path.pop(); @@ -2567,22 +2570,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err } - pub(crate) fn get_field_candidates( + pub(crate) fn get_field_candidates_considering_privacy( &self, span: Span, - base_t: Ty<'tcx>, - ) -> Option<(&[ty::FieldDef], SubstsRef<'tcx>)> { - debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_t); + base_ty: Ty<'tcx>, + mod_id: DefId, + ) -> Option<(impl Iterator + 'tcx, SubstsRef<'tcx>)> { + debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_ty); - for (base_t, _) in self.autoderef(span, base_t) { + for (base_t, _) in self.autoderef(span, base_ty) { match base_t.kind() { ty::Adt(base_def, substs) if !base_def.is_enum() => { - let fields = &base_def.non_enum_variant().fields; - // For compile-time reasons put a limit on number of fields we search - if fields.len() > 100 { - return None; - } - return Some((fields, substs)); + let tcx = self.tcx; + return Some(( + base_def + .non_enum_variant() + .fields + .iter() + .filter(move |field| field.vis.is_accessible_from(mod_id, tcx)) + // For compile-time reasons put a limit on number of fields we search + .take(100), + substs, + )); } _ => {} } @@ -2599,7 +2608,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidate_field: &ty::FieldDef, subst: SubstsRef<'tcx>, mut field_path: Vec, - id: DefId, + mod_id: DefId, ) -> Option> { debug!( "check_for_nested_field_satisfying(span: {:?}, candidate_field: {:?}, field_path: {:?}", @@ -2615,20 +2624,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let field_ty = candidate_field.ty(self.tcx, subst); if matches(candidate_field, field_ty) { return Some(field_path); - } else if let Some((nested_fields, subst)) = self.get_field_candidates(span, field_ty) { + } else if let Some((nested_fields, subst)) = + self.get_field_candidates_considering_privacy(span, field_ty, mod_id) + { // recursively search fields of `candidate_field` if it's a ty::Adt for field in nested_fields { - if field.vis.is_accessible_from(id, self.tcx) { - if let Some(field_path) = self.check_for_nested_field_satisfying( - span, - matches, - field, - subst, - field_path.clone(), - id, - ) { - return Some(field_path); - } + if let Some(field_path) = self.check_for_nested_field_satisfying( + span, + matches, + field, + subst, + field_path.clone(), + mod_id, + ) { + return Some(field_path); } } } diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index c92b93cbc22d2..ee6fe8699e129 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -1334,10 +1334,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { item_name: Ident, ) { if let SelfSource::MethodCall(expr) = source - && let Some((fields, substs)) = self.get_field_candidates(span, actual) + && let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id() + && let Some((fields, substs)) = self.get_field_candidates_considering_privacy(span, actual, mod_id) { let call_expr = self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id)); - for candidate_field in fields.iter() { + for candidate_field in fields { if let Some(field_path) = self.check_for_nested_field_satisfying( span, &|_, field_ty| { @@ -1353,7 +1354,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { candidate_field, substs, vec![], - self.tcx.parent_module(expr.hir_id).to_def_id(), + mod_id, ) { let field_path_str = field_path .iter() diff --git a/src/test/ui/issues/issue-31173.stderr b/src/test/ui/issues/issue-31173.stderr index e8797ea7b5b4c..68337a715e14f 100644 --- a/src/test/ui/issues/issue-31173.stderr +++ b/src/test/ui/issues/issue-31173.stderr @@ -33,10 +33,6 @@ LL | pub struct TakeWhile { which is required by `Cloned, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator` `Cloned, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator` which is required by `&mut Cloned, [closure@$DIR/issue-31173.rs:6:39: 6:43]>>: Iterator` -help: one of the expressions' fields has a method of the same name - | -LL | .it.collect(); - | +++ error: aborting due to 2 previous errors diff --git a/src/test/ui/issues/issue-39175.stderr b/src/test/ui/issues/issue-39175.stderr index b19f58d2a381b..afceae82e68be 100644 --- a/src/test/ui/issues/issue-39175.stderr +++ b/src/test/ui/issues/issue-39175.stderr @@ -5,10 +5,6 @@ LL | Command::new("echo").arg("hello").exec(); | ^^^^ method not found in `&mut Command` | = help: items from traits can only be used if the trait is in scope -help: one of the expressions' fields has a method of the same name - | -LL | Command::new("echo").arg("hello").inner.exec(); - | ++++++ help: the following trait is implemented but not in scope; perhaps add a `use` for it: | LL | use std::os::unix::process::CommandExt; diff --git a/src/test/ui/mismatched_types/issue-36053-2.stderr b/src/test/ui/mismatched_types/issue-36053-2.stderr index c3c8e5f272e3f..b11ea97d160be 100644 --- a/src/test/ui/mismatched_types/issue-36053-2.stderr +++ b/src/test/ui/mismatched_types/issue-36053-2.stderr @@ -35,10 +35,6 @@ LL | pub struct Filter { which is required by `Filter>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator` `Filter>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator` which is required by `&mut Filter>, [closure@$DIR/issue-36053-2.rs:7:39: 7:48]>: Iterator` -help: one of the expressions' fields has a method of the same name - | -LL | once::<&str>("str").fuse().filter(|a: &str| true).iter.count(); - | +++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/suggestions/import-trait-for-method-call.stderr b/src/test/ui/suggestions/import-trait-for-method-call.stderr index f220458f321f0..bac8de7987256 100644 --- a/src/test/ui/suggestions/import-trait-for-method-call.stderr +++ b/src/test/ui/suggestions/import-trait-for-method-call.stderr @@ -10,10 +10,6 @@ LL | fn finish(&self) -> u64; | ------ the method is available for `DefaultHasher` here | = help: items from traits can only be used if the trait is in scope -help: one of the expressions' fields has a method of the same name - | -LL | h.0.finish() - | ++ help: the following trait is implemented but not in scope; perhaps add a `use` for it: | LL | use std::hash::Hasher; diff --git a/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr b/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr index e19bc5a1fd48d..d121932c842e3 100644 --- a/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr +++ b/src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr @@ -41,16 +41,6 @@ LL | pub struct BufWriter { `&dyn std::io::Write: std::io::Write` which is required by `BufWriter<&dyn std::io::Write>: std::io::Write` = note: this error originates in the macro `writeln` (in Nightly builds, run with -Z macro-backtrace for more info) -help: one of the expressions' fields has a method of the same name - --> $SRC_DIR/core/src/macros/mod.rs:LL:COL - | -LL | $dst.inner.write_fmt($crate::format_args_nl!($($arg)*)) - | ++++++ -help: one of the expressions' fields has a method of the same name - --> $SRC_DIR/core/src/macros/mod.rs:LL:COL - | -LL | $dst.buf.write_fmt($crate::format_args_nl!($($arg)*)) - | ++++ error: aborting due to 3 previous errors diff --git a/src/test/ui/suggestions/suggest-using-chars.stderr b/src/test/ui/suggestions/suggest-using-chars.stderr index 1690309719fa7..99bcfb08a0892 100644 --- a/src/test/ui/suggestions/suggest-using-chars.stderr +++ b/src/test/ui/suggestions/suggest-using-chars.stderr @@ -25,10 +25,6 @@ help: because of the in-memory representation of `&str`, to obtain an `Iterator` | LL | let _ = String::from("bar").chars(); | ~~~~~ -help: one of the expressions' fields has a method of the same name - | -LL | let _ = String::from("bar").vec.iter(); - | ++++ error[E0599]: no method named `iter` found for reference `&String` in the current scope --> $DIR/suggest-using-chars.rs:5:36 @@ -40,10 +36,6 @@ help: because of the in-memory representation of `&str`, to obtain an `Iterator` | LL | let _ = (&String::from("bar")).chars(); | ~~~~~ -help: one of the expressions' fields has a method of the same name - | -LL | let _ = (&String::from("bar")).vec.iter(); - | ++++ error[E0599]: no method named `iter` found for type `{integer}` in the current scope --> $DIR/suggest-using-chars.rs:6:15 diff --git a/src/test/ui/unique-object-noncopyable.stderr b/src/test/ui/unique-object-noncopyable.stderr index 12917d54114e9..98a9bd07ed21d 100644 --- a/src/test/ui/unique-object-noncopyable.stderr +++ b/src/test/ui/unique-object-noncopyable.stderr @@ -23,14 +23,6 @@ LL | | >(Unique, A); which is required by `Box: Clone` `dyn Foo: Clone` which is required by `Box: Clone` -help: one of the expressions' fields has a method of the same name - | -LL | let _z = y.0.clone(); - | ++ -help: one of the expressions' fields has a method of the same name - | -LL | let _z = y.1.clone(); - | ++ error: aborting due to previous error diff --git a/src/test/ui/unique-pinned-nocopy.stderr b/src/test/ui/unique-pinned-nocopy.stderr index cc9bdd26e11c5..7af9c684b72e3 100644 --- a/src/test/ui/unique-pinned-nocopy.stderr +++ b/src/test/ui/unique-pinned-nocopy.stderr @@ -25,14 +25,6 @@ help: consider annotating `R` with `#[derive(Clone)]` | LL | #[derive(Clone)] | -help: one of the expressions' fields has a method of the same name - | -LL | let _j = i.0.clone(); - | ++ -help: one of the expressions' fields has a method of the same name - | -LL | let _j = i.1.clone(); - | ++ error: aborting due to previous error From 2a3fd5053f9cd5897c4a5eed2b742699aab279a4 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 3 Aug 2022 06:54:21 +0000 Subject: [PATCH 04/13] Don't suggest field method if it's just missing some bounds --- compiler/rustc_typeck/src/check/method/suggest.rs | 6 +++++- src/test/ui/hrtb/issue-30786.stderr | 8 -------- src/test/ui/union/union-derive-clone.mirunsafeck.stderr | 4 ---- src/test/ui/union/union-derive-clone.thirunsafeck.stderr | 4 ---- 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_typeck/src/check/method/suggest.rs b/compiler/rustc_typeck/src/check/method/suggest.rs index ee6fe8699e129..f73d0fbb27716 100644 --- a/compiler/rustc_typeck/src/check/method/suggest.rs +++ b/compiler/rustc_typeck/src/check/method/suggest.rs @@ -1000,7 +1000,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { label_span_not_found(&mut err); } - self.check_for_field_method(&mut err, source, span, actual, item_name); + // Don't suggest (for example) `expr.field.method()` if `expr.method()` + // doesn't exist due to unsatisfied predicates. + if unsatisfied_predicates.is_empty() { + self.check_for_field_method(&mut err, source, span, actual, item_name); + } self.check_for_unwrap_self(&mut err, source, span, actual, item_name); diff --git a/src/test/ui/hrtb/issue-30786.stderr b/src/test/ui/hrtb/issue-30786.stderr index bc7b5e914e127..ffe3d7b81f51e 100644 --- a/src/test/ui/hrtb/issue-30786.stderr +++ b/src/test/ui/hrtb/issue-30786.stderr @@ -18,10 +18,6 @@ note: the following trait bounds were not satisfied: | LL | impl StreamExt for T where for<'a> &'a mut T: Stream {} | --------- - ^^^^^^ unsatisfied trait bound introduced here -help: one of the expressions' fields has a method of the same name - | -LL | let filter = map.stream.filterx(|x: &_| true); - | +++++++ error[E0599]: the method `countx` exists for struct `Filter fn(&'r u64) -> &'r u64 {identity::}>, [closure@$DIR/issue-30786.rs:129:30: 129:37]>`, but its trait bounds were not satisfied --> $DIR/issue-30786.rs:130:24 @@ -43,10 +39,6 @@ note: the following trait bounds were not satisfied: | LL | impl StreamExt for T where for<'a> &'a mut T: Stream {} | --------- - ^^^^^^ unsatisfied trait bound introduced here -help: one of the expressions' fields has a method of the same name - | -LL | let count = filter.stream.countx(); - | +++++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/union/union-derive-clone.mirunsafeck.stderr b/src/test/ui/union/union-derive-clone.mirunsafeck.stderr index 44c9d4a84387e..148fb5046705b 100644 --- a/src/test/ui/union/union-derive-clone.mirunsafeck.stderr +++ b/src/test/ui/union/union-derive-clone.mirunsafeck.stderr @@ -42,10 +42,6 @@ help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]` | LL | #[derive(Clone, Copy)] | -help: one of the expressions' fields has a method of the same name - | -LL | let w = u.a.clone(); - | ++ error: aborting due to 2 previous errors diff --git a/src/test/ui/union/union-derive-clone.thirunsafeck.stderr b/src/test/ui/union/union-derive-clone.thirunsafeck.stderr index 44c9d4a84387e..148fb5046705b 100644 --- a/src/test/ui/union/union-derive-clone.thirunsafeck.stderr +++ b/src/test/ui/union/union-derive-clone.thirunsafeck.stderr @@ -42,10 +42,6 @@ help: consider annotating `CloneNoCopy` with `#[derive(Clone, Copy)]` | LL | #[derive(Clone, Copy)] | -help: one of the expressions' fields has a method of the same name - | -LL | let w = u.a.clone(); - | ++ error: aborting due to 2 previous errors From 603ffebd37a26a5b8d3c7372d432f6f2c053371d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 3 Aug 2022 07:01:18 +0000 Subject: [PATCH 05/13] Skip over structs with no private fields that impl Deref --- compiler/rustc_typeck/src/check/expr.rs | 11 ++++-- .../field-access-considering-privacy.rs | 35 +++++++++++++++++++ .../field-access-considering-privacy.stderr | 14 ++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/suggestions/field-access-considering-privacy.rs create mode 100644 src/test/ui/suggestions/field-access-considering-privacy.stderr diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs index 523a10cc36a9e..67d0e331012f2 100644 --- a/compiler/rustc_typeck/src/check/expr.rs +++ b/compiler/rustc_typeck/src/check/expr.rs @@ -2582,10 +2582,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match base_t.kind() { ty::Adt(base_def, substs) if !base_def.is_enum() => { let tcx = self.tcx; + let fields = &base_def.non_enum_variant().fields; + // Some struct, e.g. some that impl `Deref`, have all private fields + // because you're expected to deref them to access the _real_ fields. + // This, for example, will help us suggest accessing a field through a `Box`. + if fields.iter().all(|field| !field.vis.is_accessible_from(mod_id, tcx)) { + continue; + } return Some(( - base_def - .non_enum_variant() - .fields + fields .iter() .filter(move |field| field.vis.is_accessible_from(mod_id, tcx)) // For compile-time reasons put a limit on number of fields we search diff --git a/src/test/ui/suggestions/field-access-considering-privacy.rs b/src/test/ui/suggestions/field-access-considering-privacy.rs new file mode 100644 index 0000000000000..3de06b21420b4 --- /dev/null +++ b/src/test/ui/suggestions/field-access-considering-privacy.rs @@ -0,0 +1,35 @@ +use a::TyCtxt; + +mod a { + use std::ops::Deref; + pub struct TyCtxt<'tcx> { + gcx: &'tcx GlobalCtxt<'tcx>, + } + + impl<'tcx> Deref for TyCtxt<'tcx> { + type Target = &'tcx GlobalCtxt<'tcx>; + + fn deref(&self) -> &Self::Target { + &self.gcx + } + } + + pub struct GlobalCtxt<'tcx> { + pub sess: &'tcx Session, + _t: &'tcx (), + } + + pub struct Session { + pub opts: (), + } +} + +mod b { + fn foo<'tcx>(tcx: crate::TyCtxt<'tcx>) { + tcx.opts; + //~^ ERROR no field `opts` on type `TyCtxt<'tcx>` + //~| HELP one of the expressions' fields has a field of the same name + } +} + +fn main() {} diff --git a/src/test/ui/suggestions/field-access-considering-privacy.stderr b/src/test/ui/suggestions/field-access-considering-privacy.stderr new file mode 100644 index 0000000000000..cbf6f3d100258 --- /dev/null +++ b/src/test/ui/suggestions/field-access-considering-privacy.stderr @@ -0,0 +1,14 @@ +error[E0609]: no field `opts` on type `TyCtxt<'tcx>` + --> $DIR/field-access-considering-privacy.rs:29:13 + | +LL | tcx.opts; + | ^^^^ unknown field + | +help: one of the expressions' fields has a field of the same name + | +LL | tcx.sess.opts; + | +++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0609`. From 18a21e13b49320bc20836d5d20da82375fac2bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 6 Aug 2022 00:00:00 +0000 Subject: [PATCH 06/13] Remove duplicated temporaries creating during box derefs elaboration Temporaries created with `MirPatch::new_temp` will be declared after patch application. Remove manually created duplicate declarations. Removing duplicates exposes another issue. Visitor elaborates terminator twice and attempts to access new, but not yet available, local declarations. Remove duplicated call to `visit_terminator`. --- compiler/rustc_mir_transform/src/elaborate_box_derefs.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs b/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs index 44e3945d6fc89..f79d2afc0c6be 100644 --- a/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs +++ b/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs @@ -70,7 +70,6 @@ impl<'tcx, 'a> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'tcx, 'a> { build_ptr_tys(tcx, base_ty.boxed_ty(), self.unique_did, self.nonnull_did); let ptr_local = self.patch.new_temp(ptr_ty, source_info.span); - self.local_decls.push(LocalDecl::new(ptr_ty, source_info.span)); self.patch.add_statement(location, StatementKind::StorageLive(ptr_local)); @@ -125,13 +124,6 @@ impl<'tcx> MirPass<'tcx> for ElaborateBoxDerefs { index += 1; } - if let Some(terminator) = terminator - && !matches!(terminator.kind, TerminatorKind::Yield{..}) - { - let location = Location { block, statement_index: index }; - visitor.visit_terminator(terminator, location); - } - let location = Location { block, statement_index: index }; match terminator { // yielding into a box is handled when lowering generators From 7d2131a2048521ee1dfc41a12b2f5007effc1f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 6 Aug 2022 00:00:00 +0000 Subject: [PATCH 07/13] ./x.py test --bless --- .../const_prop/boxes.main.ConstProp.diff | 2 -- ...line_into_box_place.main.Inline.32bit.diff | 23 +++++++++---------- ...line_into_box_place.main.Inline.64bit.diff | 23 +++++++++---------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/test/mir-opt/const_prop/boxes.main.ConstProp.diff b/src/test/mir-opt/const_prop/boxes.main.ConstProp.diff index f2d4bee1bf94d..73fdf14049896 100644 --- a/src/test/mir-opt/const_prop/boxes.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/boxes.main.ConstProp.diff @@ -12,8 +12,6 @@ let mut _7: std::boxed::Box; // in scope 0 at $DIR/boxes.rs:+1:14: +1:22 let mut _8: *const i32; // in scope 0 at $DIR/boxes.rs:+1:14: +1:22 let mut _9: *const i32; // in scope 0 at $DIR/boxes.rs:+1:14: +1:22 - let mut _10: *const i32; // in scope 0 at $DIR/boxes.rs:+1:14: +1:22 - let mut _11: *const i32; // in scope 0 at $DIR/boxes.rs:+1:14: +1:22 scope 1 { debug x => _1; // in scope 1 at $DIR/boxes.rs:+1:9: +1:10 } diff --git a/src/test/mir-opt/inline/inline_into_box_place.main.Inline.32bit.diff b/src/test/mir-opt/inline/inline_into_box_place.main.Inline.32bit.diff index deaba70e082ed..5dfcd2d54ca27 100644 --- a/src/test/mir-opt/inline/inline_into_box_place.main.Inline.32bit.diff +++ b/src/test/mir-opt/inline/inline_into_box_place.main.Inline.32bit.diff @@ -10,15 +10,14 @@ let mut _5: std::boxed::Box>; // in scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 let mut _6: (); // in scope 0 at $DIR/inline-into-box-place.rs:+1:42: +1:43 let mut _7: *const std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 - let mut _8: *const std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 -+ let mut _9: &mut std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ let mut _8: &mut std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 scope 1 { debug _x => _1; // in scope 1 at $DIR/inline-into-box-place.rs:+1:9: +1:11 } scope 2 { } + scope 3 (inlined Vec::::new) { // at $DIR/inline-into-box-place.rs:8:33: 8:43 -+ let mut _10: alloc::raw_vec::RawVec; // in scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ let mut _9: alloc::raw_vec::RawVec; // in scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + } bb0: { @@ -37,10 +36,10 @@ StorageLive(_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 _7 = (((_5.0: std::ptr::Unique>).0: std::ptr::NonNull>).0: *const std::vec::Vec); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 - (*_7) = Vec::::new() -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 -+ StorageLive(_9); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 -+ _9 = &mut (*_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 -+ StorageLive(_10); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ _10 = const alloc::raw_vec::RawVec::::NEW; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ StorageLive(_8); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ _8 = &mut (*_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ StorageLive(_9); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ _9 = const alloc::raw_vec::RawVec::::NEW; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL // mir::Constant - // + span: $DIR/inline-into-box-place.rs:8:33: 8:41 - // + user_ty: UserType(1) @@ -51,11 +50,11 @@ + // + span: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + // + user_ty: UserType(0) + // + literal: Const { ty: alloc::raw_vec::RawVec, val: Unevaluated(alloc::raw_vec::RawVec::::NEW, [u32], None) } -+ Deinit((*_9)); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ ((*_9).0: alloc::raw_vec::RawVec) = move _10; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ ((*_9).1: usize) = const 0_usize; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ StorageDead(_10); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ StorageDead(_9); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ Deinit((*_8)); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ ((*_8).0: alloc::raw_vec::RawVec) = move _9; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ ((*_8).1: usize) = const 0_usize; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ StorageDead(_9); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ StorageDead(_8); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 StorageDead(_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 _1 = move _5; // scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 StorageDead(_5); // scope 0 at $DIR/inline-into-box-place.rs:+1:42: +1:43 diff --git a/src/test/mir-opt/inline/inline_into_box_place.main.Inline.64bit.diff b/src/test/mir-opt/inline/inline_into_box_place.main.Inline.64bit.diff index deaba70e082ed..5dfcd2d54ca27 100644 --- a/src/test/mir-opt/inline/inline_into_box_place.main.Inline.64bit.diff +++ b/src/test/mir-opt/inline/inline_into_box_place.main.Inline.64bit.diff @@ -10,15 +10,14 @@ let mut _5: std::boxed::Box>; // in scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 let mut _6: (); // in scope 0 at $DIR/inline-into-box-place.rs:+1:42: +1:43 let mut _7: *const std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 - let mut _8: *const std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 -+ let mut _9: &mut std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ let mut _8: &mut std::vec::Vec; // in scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 scope 1 { debug _x => _1; // in scope 1 at $DIR/inline-into-box-place.rs:+1:9: +1:11 } scope 2 { } + scope 3 (inlined Vec::::new) { // at $DIR/inline-into-box-place.rs:8:33: 8:43 -+ let mut _10: alloc::raw_vec::RawVec; // in scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ let mut _9: alloc::raw_vec::RawVec; // in scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + } bb0: { @@ -37,10 +36,10 @@ StorageLive(_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 _7 = (((_5.0: std::ptr::Unique>).0: std::ptr::NonNull>).0: *const std::vec::Vec); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 - (*_7) = Vec::::new() -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 -+ StorageLive(_9); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 -+ _9 = &mut (*_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 -+ StorageLive(_10); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ _10 = const alloc::raw_vec::RawVec::::NEW; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ StorageLive(_8); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ _8 = &mut (*_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ StorageLive(_9); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ _9 = const alloc::raw_vec::RawVec::::NEW; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL // mir::Constant - // + span: $DIR/inline-into-box-place.rs:8:33: 8:41 - // + user_ty: UserType(1) @@ -51,11 +50,11 @@ + // + span: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + // + user_ty: UserType(0) + // + literal: Const { ty: alloc::raw_vec::RawVec, val: Unevaluated(alloc::raw_vec::RawVec::::NEW, [u32], None) } -+ Deinit((*_9)); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ ((*_9).0: alloc::raw_vec::RawVec) = move _10; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ ((*_9).1: usize) = const 0_usize; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ StorageDead(_10); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL -+ StorageDead(_9); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 ++ Deinit((*_8)); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ ((*_8).0: alloc::raw_vec::RawVec) = move _9; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ ((*_8).1: usize) = const 0_usize; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ StorageDead(_9); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL ++ StorageDead(_8); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 StorageDead(_7); // scope 0 at $DIR/inline-into-box-place.rs:+1:33: +1:43 _1 = move _5; // scope 0 at $DIR/inline-into-box-place.rs:+1:29: +1:43 StorageDead(_5); // scope 0 at $DIR/inline-into-box-place.rs:+1:42: +1:43 From db7ddc50b62f4c0a687e591b69ebe224c8eafadb Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 7 Aug 2022 12:36:54 +0200 Subject: [PATCH 08/13] Do not manually craft a span pointing inside a multibyte character. --- compiler/rustc_lint/src/unused.rs | 88 +++++++++---------- .../lint/unused_parens_multibyte_recovery.rs | 11 +++ .../unused_parens_multibyte_recovery.stderr | 43 +++++++++ 3 files changed, 96 insertions(+), 46 deletions(-) create mode 100644 src/test/ui/lint/unused_parens_multibyte_recovery.rs create mode 100644 src/test/ui/lint/unused_parens_multibyte_recovery.stderr diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index b6cf182916cb9..4e7ba1c6ce4fa 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -11,7 +11,7 @@ use rustc_middle::ty::adjustment; use rustc_middle::ty::{self, Ty}; use rustc_span::symbol::Symbol; use rustc_span::symbol::{kw, sym}; -use rustc_span::{BytePos, Span, DUMMY_SP}; +use rustc_span::{BytePos, Span}; declare_lint! { /// The `unused_must_use` lint detects unused result of a type flagged as @@ -504,23 +504,23 @@ trait UnusedDelimLint { ast::ExprKind::Block(ref block, None) if block.stmts.len() > 0 => { let start = block.stmts[0].span; let end = block.stmts[block.stmts.len() - 1].span; - if value.span.from_expansion() || start.from_expansion() || end.from_expansion() { - ( - value.span.with_hi(value.span.lo() + BytePos(1)), - value.span.with_lo(value.span.hi() - BytePos(1)), - ) + if let Some(start) = start.find_ancestor_inside(value.span) + && let Some(end) = end.find_ancestor_inside(value.span) + { + Some(( + value.span.with_hi(start.lo()), + value.span.with_lo(end.hi()), + )) } else { - (value.span.with_hi(start.lo()), value.span.with_lo(end.hi())) + None } } ast::ExprKind::Paren(ref expr) => { - if value.span.from_expansion() || expr.span.from_expansion() { - ( - value.span.with_hi(value.span.lo() + BytePos(1)), - value.span.with_lo(value.span.hi() - BytePos(1)), - ) + let expr_span = expr.span.find_ancestor_inside(value.span); + if let Some(expr_span) = expr_span { + Some((value.span.with_hi(expr_span.lo()), value.span.with_lo(expr_span.hi()))) } else { - (value.span.with_hi(expr.span.lo()), value.span.with_lo(expr.span.hi())) + None } } _ => return, @@ -529,36 +529,38 @@ trait UnusedDelimLint { left_pos.map_or(false, |s| s >= value.span.lo()), right_pos.map_or(false, |s| s <= value.span.hi()), ); - self.emit_unused_delims(cx, spans, ctx.into(), keep_space); + self.emit_unused_delims(cx, value.span, spans, ctx.into(), keep_space); } fn emit_unused_delims( &self, cx: &EarlyContext<'_>, - spans: (Span, Span), + value_span: Span, + spans: Option<(Span, Span)>, msg: &str, keep_space: (bool, bool), ) { - // FIXME(flip1995): Quick and dirty fix for #70814. This should be fixed in rustdoc - // properly. - if spans.0 == DUMMY_SP || spans.1 == DUMMY_SP { - return; - } - - cx.struct_span_lint(self.lint(), MultiSpan::from(vec![spans.0, spans.1]), |lint| { - let replacement = vec![ - (spans.0, if keep_space.0 { " ".into() } else { "".into() }), - (spans.1, if keep_space.1 { " ".into() } else { "".into() }), - ]; - lint.build(fluent::lint::unused_delim) - .set_arg("delim", Self::DELIM_STR) - .set_arg("item", msg) - .multipart_suggestion( + let primary_span = if let Some((lo, hi)) = spans { + MultiSpan::from(vec![lo, hi]) + } else { + MultiSpan::from(value_span) + }; + cx.struct_span_lint(self.lint(), primary_span, |lint| { + let mut db = lint.build(fluent::lint::unused_delim); + db.set_arg("delim", Self::DELIM_STR); + db.set_arg("item", msg); + if let Some((lo, hi)) = spans { + let replacement = vec![ + (lo, if keep_space.0 { " ".into() } else { "".into() }), + (hi, if keep_space.1 { " ".into() } else { "".into() }), + ]; + db.multipart_suggestion( fluent::lint::suggestion, replacement, Applicability::MachineApplicable, - ) - .emit(); + ); + } + db.emit(); }); } @@ -766,15 +768,12 @@ impl UnusedParens { // Otherwise proceed with linting. _ => {} } - let spans = if value.span.from_expansion() || inner.span.from_expansion() { - ( - value.span.with_hi(value.span.lo() + BytePos(1)), - value.span.with_lo(value.span.hi() - BytePos(1)), - ) + let spans = if let Some(inner) = inner.span.find_ancestor_inside(value.span) { + Some((value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi()))) } else { - (value.span.with_hi(inner.span.lo()), value.span.with_lo(inner.span.hi())) + None }; - self.emit_unused_delims(cx, spans, "pattern", (false, false)); + self.emit_unused_delims(cx, value.span, spans, "pattern", (false, false)); } } } @@ -879,15 +878,12 @@ impl EarlyLintPass for UnusedParens { ); } _ => { - let spans = if ty.span.from_expansion() || r.span.from_expansion() { - ( - ty.span.with_hi(ty.span.lo() + BytePos(1)), - ty.span.with_lo(ty.span.hi() - BytePos(1)), - ) + let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) { + Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi()))) } else { - (ty.span.with_hi(r.span.lo()), ty.span.with_lo(r.span.hi())) + None }; - self.emit_unused_delims(cx, spans, "type", (false, false)); + self.emit_unused_delims(cx, ty.span, spans, "type", (false, false)); } } } diff --git a/src/test/ui/lint/unused_parens_multibyte_recovery.rs b/src/test/ui/lint/unused_parens_multibyte_recovery.rs new file mode 100644 index 0000000000000..8fcfae22a3d33 --- /dev/null +++ b/src/test/ui/lint/unused_parens_multibyte_recovery.rs @@ -0,0 +1,11 @@ +// ignore-tidy-trailing-newlines +// +// error-pattern: this file contains an unclosed delimiter +// error-pattern: this file contains an unclosed delimiter +// error-pattern: this file contains an unclosed delimiter +// error-pattern: format argument must be a string literal +// +// Verify that unused parens lint does not try to create a span +// which points in the middle of a multibyte character. + +fn f(){(print!(á \ No newline at end of file diff --git a/src/test/ui/lint/unused_parens_multibyte_recovery.stderr b/src/test/ui/lint/unused_parens_multibyte_recovery.stderr new file mode 100644 index 0000000000000..a0302b17e255d --- /dev/null +++ b/src/test/ui/lint/unused_parens_multibyte_recovery.stderr @@ -0,0 +1,43 @@ +error: this file contains an unclosed delimiter + --> $DIR/unused_parens_multibyte_recovery.rs:11:17 + | +LL | fn f(){(print!(á + | -- - ^ + | || | + | || unclosed delimiter + | |unclosed delimiter + | unclosed delimiter + +error: this file contains an unclosed delimiter + --> $DIR/unused_parens_multibyte_recovery.rs:11:17 + | +LL | fn f(){(print!(á + | -- - ^ + | || | + | || unclosed delimiter + | |unclosed delimiter + | unclosed delimiter + +error: this file contains an unclosed delimiter + --> $DIR/unused_parens_multibyte_recovery.rs:11:17 + | +LL | fn f(){(print!(á + | -- - ^ + | || | + | || unclosed delimiter + | |unclosed delimiter + | unclosed delimiter + +error: format argument must be a string literal + --> $DIR/unused_parens_multibyte_recovery.rs:11:16 + | +LL | fn f(){(print!(á + | ^ + | +help: you might be missing a string literal to format with + | +LL | fn f(){(print!("{}", á + | +++++ + +error: aborting due to 4 previous errors + From ac7586388819d0076945f55440b1dd500bd07dde Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Tue, 2 Aug 2022 17:44:25 -0700 Subject: [PATCH 09/13] Add more `// unit-test`s to MIR opt tests --- src/test/mir-opt/bool_compare.rs | 2 + ...ine_array_len.norm2.InstCombine.64bit.diff | 77 ---------------- ... combine_array_len.norm2.InstCombine.diff} | 0 src/test/mir-opt/combine_array_len.rs | 2 +- src/test/mir-opt/const_goto.rs | 2 + ...oto_storage.match_nested_if.ConstGoto.diff | 2 +- src/test/mir-opt/const_goto_storage.rs | 2 + src/test/mir-opt/deaggregator_test.rs | 2 + src/test/mir-opt/deaggregator_test_enum.rs | 2 + src/test/mir-opt/deaggregator_test_enum_2.rs | 1 + .../mir-opt/deaggregator_test_multiple.rs | 1 + ..._line_doc_comment_2.DeduplicateBlocks.diff | 89 +++++++++---------- src/test/mir-opt/deduplicate_blocks.rs | 2 + .../derefer_complex_case.main.Derefer.diff | 8 +- src/test/mir-opt/derefer_complex_case.rs | 1 + .../derefer_inline_test.main.Derefer.diff | 4 +- src/test/mir-opt/derefer_inline_test.rs | 1 + .../derefer_terminator_test.main.Derefer.diff | 4 +- src/test/mir-opt/derefer_terminator_test.rs | 1 + src/test/mir-opt/derefer_test.rs | 1 + src/test/mir-opt/derefer_test_multiple.rs | 1 + src/test/mir-opt/equal_true.rs | 2 + src/test/mir-opt/instrument_coverage.rs | 1 + src/test/mir-opt/not_equal_false.rs | 1 + ...age_markers.main.RemoveStorageMarkers.diff | 50 +++++------ src/test/mir-opt/remove_storage_markers.rs | 2 + .../mir-opt/remove_zsts_dont_touch_unions.rs | 2 +- ...asts.SimplifyCfg-elaborate-drops.after.mir | 4 +- ...main.SimplifyCfg-elaborate-drops.after.mir | 8 +- src/test/mir-opt/retag.rs | 1 + ...-foo.SimplifyCfg-elaborate-drops.after.mir | 4 +- ..._shr.SimplifyCfg-elaborate-drops.after.mir | 4 +- .../simplify-locals-removes-unused-consts.rs | 1 + ...ves_unused_consts.main.SimplifyLocals.diff | 54 +++++++++-- 34 files changed, 160 insertions(+), 179 deletions(-) delete mode 100644 src/test/mir-opt/combine_array_len.norm2.InstCombine.64bit.diff rename src/test/mir-opt/{combine_array_len.norm2.InstCombine.32bit.diff => combine_array_len.norm2.InstCombine.diff} (100%) diff --git a/src/test/mir-opt/bool_compare.rs b/src/test/mir-opt/bool_compare.rs index 3ff046325dc1f..4435bf5b0f2c8 100644 --- a/src/test/mir-opt/bool_compare.rs +++ b/src/test/mir-opt/bool_compare.rs @@ -1,3 +1,5 @@ +// unit-test: InstCombine + // EMIT_MIR bool_compare.opt1.InstCombine.diff fn opt1(x: bool) -> u32 { if x != true { 0 } else { 1 } diff --git a/src/test/mir-opt/combine_array_len.norm2.InstCombine.64bit.diff b/src/test/mir-opt/combine_array_len.norm2.InstCombine.64bit.diff deleted file mode 100644 index c73150f947dfe..0000000000000 --- a/src/test/mir-opt/combine_array_len.norm2.InstCombine.64bit.diff +++ /dev/null @@ -1,77 +0,0 @@ -- // MIR for `norm2` before InstCombine -+ // MIR for `norm2` after InstCombine - - fn norm2(_1: [f32; 2]) -> f32 { - debug x => _1; // in scope 0 at $DIR/combine_array_len.rs:+0:10: +0:11 - let mut _0: f32; // return place in scope 0 at $DIR/combine_array_len.rs:+0:26: +0:29 - let _2: f32; // in scope 0 at $DIR/combine_array_len.rs:+1:9: +1:10 - let _3: usize; // in scope 0 at $DIR/combine_array_len.rs:+1:15: +1:16 - let mut _4: usize; // in scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 - let mut _5: bool; // in scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 - let _7: usize; // in scope 0 at $DIR/combine_array_len.rs:+2:15: +2:16 - let mut _8: usize; // in scope 0 at $DIR/combine_array_len.rs:+2:13: +2:17 - let mut _9: bool; // in scope 0 at $DIR/combine_array_len.rs:+2:13: +2:17 - let mut _10: f32; // in scope 0 at $DIR/combine_array_len.rs:+3:5: +3:8 - let mut _11: f32; // in scope 0 at $DIR/combine_array_len.rs:+3:5: +3:6 - let mut _12: f32; // in scope 0 at $DIR/combine_array_len.rs:+3:7: +3:8 - let mut _13: f32; // in scope 0 at $DIR/combine_array_len.rs:+3:11: +3:14 - let mut _14: f32; // in scope 0 at $DIR/combine_array_len.rs:+3:11: +3:12 - let mut _15: f32; // in scope 0 at $DIR/combine_array_len.rs:+3:13: +3:14 - scope 1 { - debug a => _2; // in scope 1 at $DIR/combine_array_len.rs:+1:9: +1:10 - let _6: f32; // in scope 1 at $DIR/combine_array_len.rs:+2:9: +2:10 - scope 2 { - debug b => _6; // in scope 2 at $DIR/combine_array_len.rs:+2:9: +2:10 - } - } - - bb0: { - StorageLive(_2); // scope 0 at $DIR/combine_array_len.rs:+1:9: +1:10 - StorageLive(_3); // scope 0 at $DIR/combine_array_len.rs:+1:15: +1:16 - _3 = const 0_usize; // scope 0 at $DIR/combine_array_len.rs:+1:15: +1:16 -- _4 = Len(_1); // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 -+ _4 = const 2_usize; // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 - _5 = Lt(_3, _4); // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 - assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> bb1; // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 - } - - bb1: { - _2 = _1[_3]; // scope 0 at $DIR/combine_array_len.rs:+1:13: +1:17 - StorageDead(_3); // scope 0 at $DIR/combine_array_len.rs:+1:17: +1:18 - StorageLive(_6); // scope 1 at $DIR/combine_array_len.rs:+2:9: +2:10 - StorageLive(_7); // scope 1 at $DIR/combine_array_len.rs:+2:15: +2:16 - _7 = const 1_usize; // scope 1 at $DIR/combine_array_len.rs:+2:15: +2:16 -- _8 = Len(_1); // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17 -+ _8 = const 2_usize; // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17 - _9 = Lt(_7, _8); // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17 - assert(move _9, "index out of bounds: the length is {} but the index is {}", move _8, _7) -> bb2; // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17 - } - - bb2: { - _6 = _1[_7]; // scope 1 at $DIR/combine_array_len.rs:+2:13: +2:17 - StorageDead(_7); // scope 1 at $DIR/combine_array_len.rs:+2:17: +2:18 - StorageLive(_10); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:8 - StorageLive(_11); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:6 - _11 = _2; // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:6 - StorageLive(_12); // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8 - _12 = _2; // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8 - _10 = Mul(move _11, move _12); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:8 - StorageDead(_12); // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8 - StorageDead(_11); // scope 2 at $DIR/combine_array_len.rs:+3:7: +3:8 - StorageLive(_13); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:14 - StorageLive(_14); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:12 - _14 = _6; // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:12 - StorageLive(_15); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14 - _15 = _6; // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14 - _13 = Mul(move _14, move _15); // scope 2 at $DIR/combine_array_len.rs:+3:11: +3:14 - StorageDead(_15); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14 - StorageDead(_14); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14 - _0 = Add(move _10, move _13); // scope 2 at $DIR/combine_array_len.rs:+3:5: +3:14 - StorageDead(_13); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14 - StorageDead(_10); // scope 2 at $DIR/combine_array_len.rs:+3:13: +3:14 - StorageDead(_6); // scope 1 at $DIR/combine_array_len.rs:+4:1: +4:2 - StorageDead(_2); // scope 0 at $DIR/combine_array_len.rs:+4:1: +4:2 - return; // scope 0 at $DIR/combine_array_len.rs:+4:2: +4:2 - } - } - diff --git a/src/test/mir-opt/combine_array_len.norm2.InstCombine.32bit.diff b/src/test/mir-opt/combine_array_len.norm2.InstCombine.diff similarity index 100% rename from src/test/mir-opt/combine_array_len.norm2.InstCombine.32bit.diff rename to src/test/mir-opt/combine_array_len.norm2.InstCombine.diff diff --git a/src/test/mir-opt/combine_array_len.rs b/src/test/mir-opt/combine_array_len.rs index 93490c14fd645..3ef3bd09afdef 100644 --- a/src/test/mir-opt/combine_array_len.rs +++ b/src/test/mir-opt/combine_array_len.rs @@ -1,4 +1,4 @@ -// EMIT_MIR_FOR_EACH_BIT_WIDTH +// unit-test: InstCombine // EMIT_MIR combine_array_len.norm2.InstCombine.diff fn norm2(x: [f32; 2]) -> f32 { diff --git a/src/test/mir-opt/const_goto.rs b/src/test/mir-opt/const_goto.rs index 939902e70e94b..6f84f186b3115 100644 --- a/src/test/mir-opt/const_goto.rs +++ b/src/test/mir-opt/const_goto.rs @@ -1,3 +1,5 @@ +// unit-test: ConstGoto + pub enum Foo { A, B, diff --git a/src/test/mir-opt/const_goto_storage.match_nested_if.ConstGoto.diff b/src/test/mir-opt/const_goto_storage.match_nested_if.ConstGoto.diff index 2b09ef7866126..81c356cb1db52 100644 --- a/src/test/mir-opt/const_goto_storage.match_nested_if.ConstGoto.diff +++ b/src/test/mir-opt/const_goto_storage.match_nested_if.ConstGoto.diff @@ -17,7 +17,7 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/const_goto_storage.rs:+1:9: +1:12 - StorageLive(_2); // scope 0 at $DIR/const_goto_storage.rs:+1:21: +1:23 -- nop; // scope 0 at $DIR/const_goto_storage.rs:+1:21: +1:23 +- Deinit(_2); // scope 0 at $DIR/const_goto_storage.rs:+1:21: +1:23 - StorageLive(_3); // scope 0 at $DIR/const_goto_storage.rs:+2:15: +6:10 - StorageLive(_4); // scope 0 at $DIR/const_goto_storage.rs:+2:18: +2:76 - StorageLive(_5); // scope 0 at $DIR/const_goto_storage.rs:+2:21: +2:52 diff --git a/src/test/mir-opt/const_goto_storage.rs b/src/test/mir-opt/const_goto_storage.rs index 4ef68e7e1faf0..459599c73eb9c 100644 --- a/src/test/mir-opt/const_goto_storage.rs +++ b/src/test/mir-opt/const_goto_storage.rs @@ -1,3 +1,5 @@ +// unit-test: ConstGoto + // EMIT_MIR const_goto_storage.match_nested_if.ConstGoto.diff fn match_nested_if() -> bool { let val = match () { diff --git a/src/test/mir-opt/deaggregator_test.rs b/src/test/mir-opt/deaggregator_test.rs index 342e222431b49..ee59402af386b 100644 --- a/src/test/mir-opt/deaggregator_test.rs +++ b/src/test/mir-opt/deaggregator_test.rs @@ -1,3 +1,5 @@ +// unit-test: Deaggregator + struct Baz { x: usize, y: f32, diff --git a/src/test/mir-opt/deaggregator_test_enum.rs b/src/test/mir-opt/deaggregator_test_enum.rs index 02b63a1f55d4e..ea402dafdec7a 100644 --- a/src/test/mir-opt/deaggregator_test_enum.rs +++ b/src/test/mir-opt/deaggregator_test_enum.rs @@ -1,3 +1,5 @@ +// unit-test: Deaggregator + enum Baz { Empty, Foo { x: usize }, diff --git a/src/test/mir-opt/deaggregator_test_enum_2.rs b/src/test/mir-opt/deaggregator_test_enum_2.rs index 489854ff0aa3d..955c317324aa8 100644 --- a/src/test/mir-opt/deaggregator_test_enum_2.rs +++ b/src/test/mir-opt/deaggregator_test_enum_2.rs @@ -1,3 +1,4 @@ +// unit-test: Deaggregator // Test that deaggregate fires in more than one basic block enum Foo { diff --git a/src/test/mir-opt/deaggregator_test_multiple.rs b/src/test/mir-opt/deaggregator_test_multiple.rs index 9730b9aa8e5c0..46305fe21d28c 100644 --- a/src/test/mir-opt/deaggregator_test_multiple.rs +++ b/src/test/mir-opt/deaggregator_test_multiple.rs @@ -1,3 +1,4 @@ +// unit-test: Deaggregator // Test that deaggregate fires more than once per block enum Foo { diff --git a/src/test/mir-opt/deduplicate_blocks.is_line_doc_comment_2.DeduplicateBlocks.diff b/src/test/mir-opt/deduplicate_blocks.is_line_doc_comment_2.DeduplicateBlocks.diff index 01864ba24ac08..53f977de5d6ad 100644 --- a/src/test/mir-opt/deduplicate_blocks.is_line_doc_comment_2.DeduplicateBlocks.diff +++ b/src/test/mir-opt/deduplicate_blocks.is_line_doc_comment_2.DeduplicateBlocks.diff @@ -7,101 +7,94 @@ let mut _2: &[u8]; // in scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 let mut _3: &str; // in scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 let mut _4: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 - let mut _5: bool; // in scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 - let mut _6: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 - let mut _7: bool; // in scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 - scope 1 (inlined core::str::::as_bytes) { // at $DIR/deduplicate_blocks.rs:3:11: 3:23 - debug self => _3; // in scope 1 at $SRC_DIR/core/src/str/mod.rs:LL:COL - let mut _8: &str; // in scope 1 at $SRC_DIR/core/src/str/mod.rs:LL:COL - scope 2 { - } - } + let mut _5: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 + let mut _6: bool; // in scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 + let mut _7: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 + let mut _8: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 + let mut _9: bool; // in scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 bb0: { StorageLive(_2); // scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 StorageLive(_3); // scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 - _3 = _1; // scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 - StorageLive(_8); // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL - _8 = _3; // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL -- _2 = transmute::<&str, &[u8]>(move _8) -> bb14; // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL -+ _2 = transmute::<&str, &[u8]>(move _8) -> bb12; // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL + _3 = &(*_1); // scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 + _2 = core::str::::as_bytes(move _3) -> bb1; // scope 0 at $DIR/deduplicate_blocks.rs:+1:11: +1:23 // mir::Constant - // + span: $SRC_DIR/core/src/str/mod.rs:LL:COL - // + literal: Const { ty: unsafe extern "rust-intrinsic" fn(&str) -> &[u8] {transmute::<&str, &[u8]>}, val: Value() } + // + span: $DIR/deduplicate_blocks.rs:5:13: 5:21 + // + literal: Const { ty: for<'r> fn(&'r str) -> &'r [u8] {core::str::::as_bytes}, val: Value() } } bb1: { - switchInt((*_2)[0 of 4]) -> [47_u8: bb2, otherwise: bb5]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + StorageDead(_3); // scope 0 at $DIR/deduplicate_blocks.rs:+1:22: +1:23 + _7 = Len((*_2)); // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 + _8 = const 4_usize; // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 + _9 = Ge(move _7, move _8); // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 + switchInt(move _9) -> [false: bb6, otherwise: bb2]; // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 } bb2: { - switchInt((*_2)[1 of 4]) -> [47_u8: bb3, otherwise: bb5]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + switchInt((*_2)[0 of 4]) -> [47_u8: bb3, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 } bb3: { - switchInt((*_2)[2 of 4]) -> [47_u8: bb4, otherwise: bb5]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + switchInt((*_2)[1 of 4]) -> [47_u8: bb4, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 } bb4: { -- switchInt((*_2)[3 of 4]) -> [47_u8: bb10, otherwise: bb5]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 -+ switchInt((*_2)[3 of 4]) -> [47_u8: bb9, otherwise: bb5]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + switchInt((*_2)[2 of 4]) -> [47_u8: bb5, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 } bb5: { - _4 = Len((*_2)); // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 - _5 = Ge(move _4, const 3_usize); // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 - switchInt(move _5) -> [false: bb9, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 +- switchInt((*_2)[3 of 4]) -> [47_u8: bb11, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 ++ switchInt((*_2)[3 of 4]) -> [47_u8: bb10, otherwise: bb6]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 } bb6: { - switchInt((*_2)[0 of 3]) -> [47_u8: bb7, otherwise: bb9]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + _4 = Len((*_2)); // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 + _5 = const 3_usize; // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 + _6 = Ge(move _4, move _5); // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 + switchInt(move _6) -> [false: bb10, otherwise: bb7]; // scope 0 at $DIR/deduplicate_blocks.rs:+3:9: +3:31 } bb7: { - switchInt((*_2)[1 of 3]) -> [47_u8: bb8, otherwise: bb9]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + switchInt((*_2)[0 of 3]) -> [47_u8: bb8, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 } bb8: { -- switchInt((*_2)[2 of 3]) -> [47_u8: bb11, 33_u8: bb12, otherwise: bb9]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 -+ switchInt((*_2)[2 of 3]) -> [47_u8: bb10, 33_u8: bb10, otherwise: bb9]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + switchInt((*_2)[1 of 3]) -> [47_u8: bb9, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 } bb9: { +- switchInt((*_2)[2 of 3]) -> [47_u8: bb12, 33_u8: bb13, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 ++ switchInt((*_2)[2 of 3]) -> [47_u8: bb11, 33_u8: bb11, otherwise: bb10]; // scope 0 at $DIR/deduplicate_blocks.rs:+1:5: +1:23 + } + + bb10: { - _0 = const false; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19 -- goto -> bb13; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19 +- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+5:14: +5:19 - } - -- bb10: { +- bb11: { _0 = const false; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46 -- goto -> bb13; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46 -+ goto -> bb11; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46 +- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46 ++ goto -> bb12; // scope 0 at $DIR/deduplicate_blocks.rs:+2:41: +2:46 } -- bb11: { +- bb12: { - _0 = const true; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39 -- goto -> bb13; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39 +- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+3:35: +3:39 - } - -- bb12: { -+ bb10: { - _0 = const true; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39 -- goto -> bb13; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39 -+ goto -> bb11; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39 - } - - bb13: { + bb11: { - StorageDead(_2); // scope 0 at $DIR/deduplicate_blocks.rs:+7:1: +7:2 - return; // scope 0 at $DIR/deduplicate_blocks.rs:+7:2: +7:2 + _0 = const true; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39 +- goto -> bb14; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39 ++ goto -> bb12; // scope 0 at $DIR/deduplicate_blocks.rs:+4:35: +4:39 } - bb14: { + bb12: { - StorageDead(_8); // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL - StorageDead(_3); // scope 0 at $DIR/deduplicate_blocks.rs:+1:22: +1:23 - _6 = Len((*_2)); // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 - _7 = Ge(move _6, const 4_usize); // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 - switchInt(move _7) -> [false: bb5, otherwise: bb1]; // scope 0 at $DIR/deduplicate_blocks.rs:+2:9: +2:37 + StorageDead(_2); // scope 0 at $DIR/deduplicate_blocks.rs:+7:1: +7:2 + return; // scope 0 at $DIR/deduplicate_blocks.rs:+7:2: +7:2 } } diff --git a/src/test/mir-opt/deduplicate_blocks.rs b/src/test/mir-opt/deduplicate_blocks.rs index f8f7361dc0d0e..2b9eed99ecdbf 100644 --- a/src/test/mir-opt/deduplicate_blocks.rs +++ b/src/test/mir-opt/deduplicate_blocks.rs @@ -1,3 +1,5 @@ +// unit-test: DeduplicateBlocks + // EMIT_MIR deduplicate_blocks.is_line_doc_comment_2.DeduplicateBlocks.diff pub const fn is_line_doc_comment_2(s: &str) -> bool { match s.as_bytes() { diff --git a/src/test/mir-opt/derefer_complex_case.main.Derefer.diff b/src/test/mir-opt/derefer_complex_case.main.Derefer.diff index de0c03bb70b7e..abd6193fed957 100644 --- a/src/test/mir-opt/derefer_complex_case.main.Derefer.diff +++ b/src/test/mir-opt/derefer_complex_case.main.Derefer.diff @@ -30,12 +30,12 @@ StorageLive(_2); // scope 0 at $DIR/derefer_complex_case.rs:+1:17: +1:26 _14 = const main::promoted[0]; // scope 0 at $DIR/derefer_complex_case.rs:+1:17: +1:26 // mir::Constant - // + span: $DIR/derefer_complex_case.rs:5:17: 5:26 + // + span: $DIR/derefer_complex_case.rs:6:17: 6:26 // + literal: Const { ty: &[i32; 2], val: Unevaluated(main, [], Some(promoted[0])) } _2 = &(*_14); // scope 0 at $DIR/derefer_complex_case.rs:+1:17: +1:26 _1 = <&[i32; 2] as IntoIterator>::into_iter(move _2) -> bb1; // scope 0 at $DIR/derefer_complex_case.rs:+1:17: +1:26 // mir::Constant - // + span: $DIR/derefer_complex_case.rs:5:17: 5:26 + // + span: $DIR/derefer_complex_case.rs:6:17: 6:26 // + literal: Const { ty: fn(&[i32; 2]) -> <&[i32; 2] as IntoIterator>::IntoIter {<&[i32; 2] as IntoIterator>::into_iter}, val: Value() } } @@ -55,7 +55,7 @@ _8 = &mut (*_9); // scope 1 at $DIR/derefer_complex_case.rs:+1:17: +1:26 _7 = as Iterator>::next(move _8) -> bb3; // scope 1 at $DIR/derefer_complex_case.rs:+1:17: +1:26 // mir::Constant - // + span: $DIR/derefer_complex_case.rs:5:17: 5:26 + // + span: $DIR/derefer_complex_case.rs:6:17: 6:26 // + literal: Const { ty: for<'r> fn(&'r mut std::slice::Iter) -> Option< as Iterator>::Item> { as Iterator>::next}, val: Value() } } @@ -76,7 +76,7 @@ _13 = _12; // scope 2 at $DIR/derefer_complex_case.rs:+1:34: +1:37 _6 = std::mem::drop::(move _13) -> bb7; // scope 2 at $DIR/derefer_complex_case.rs:+1:29: +1:38 // mir::Constant - // + span: $DIR/derefer_complex_case.rs:5:29: 5:33 + // + span: $DIR/derefer_complex_case.rs:6:29: 6:33 // + literal: Const { ty: fn(i32) {std::mem::drop::}, val: Value() } } diff --git a/src/test/mir-opt/derefer_complex_case.rs b/src/test/mir-opt/derefer_complex_case.rs index 48bec39074c89..dc48cee950bc9 100644 --- a/src/test/mir-opt/derefer_complex_case.rs +++ b/src/test/mir-opt/derefer_complex_case.rs @@ -1,3 +1,4 @@ +// unit-test: Derefer // EMIT_MIR derefer_complex_case.main.Derefer.diff // ignore-wasm32 diff --git a/src/test/mir-opt/derefer_inline_test.main.Derefer.diff b/src/test/mir-opt/derefer_inline_test.main.Derefer.diff index ce6ffaa56413c..3540df3083670 100644 --- a/src/test/mir-opt/derefer_inline_test.main.Derefer.diff +++ b/src/test/mir-opt/derefer_inline_test.main.Derefer.diff @@ -17,7 +17,7 @@ _3 = AlignOf(std::boxed::Box); // scope 1 at $DIR/derefer_inline_test.rs:+1:5: +1:12 _4 = alloc::alloc::exchange_malloc(move _2, move _3) -> bb1; // scope 1 at $DIR/derefer_inline_test.rs:+1:5: +1:12 // mir::Constant - // + span: $DIR/derefer_inline_test.rs:10:5: 10:12 + // + span: $DIR/derefer_inline_test.rs:11:5: 11:12 // + literal: Const { ty: unsafe fn(usize, usize) -> *mut u8 {alloc::alloc::exchange_malloc}, val: Value() } } @@ -26,7 +26,7 @@ _5 = ShallowInitBox(move _4, std::boxed::Box); // scope 0 at $DIR/derefer_inline_test.rs:+1:5: +1:12 (*_5) = f() -> [return: bb2, unwind: bb6]; // scope 0 at $DIR/derefer_inline_test.rs:+1:9: +1:12 // mir::Constant - // + span: $DIR/derefer_inline_test.rs:10:9: 10:10 + // + span: $DIR/derefer_inline_test.rs:11:9: 11:10 // + literal: Const { ty: fn() -> Box {f}, val: Value() } } diff --git a/src/test/mir-opt/derefer_inline_test.rs b/src/test/mir-opt/derefer_inline_test.rs index 191a8cbbef40a..cc06a7dd8c40a 100644 --- a/src/test/mir-opt/derefer_inline_test.rs +++ b/src/test/mir-opt/derefer_inline_test.rs @@ -1,3 +1,4 @@ +// unit-test: Derefer // EMIT_MIR derefer_inline_test.main.Derefer.diff // ignore-wasm32 compiled with panic=abort by default diff --git a/src/test/mir-opt/derefer_terminator_test.main.Derefer.diff b/src/test/mir-opt/derefer_terminator_test.main.Derefer.diff index 0a56ee5e454a3..ed336208325fe 100644 --- a/src/test/mir-opt/derefer_terminator_test.main.Derefer.diff +++ b/src/test/mir-opt/derefer_terminator_test.main.Derefer.diff @@ -32,7 +32,7 @@ StorageLive(_1); // scope 0 at $DIR/derefer_terminator_test.rs:+1:9: +1:10 _1 = foo() -> bb1; // scope 0 at $DIR/derefer_terminator_test.rs:+1:13: +1:18 // mir::Constant - // + span: $DIR/derefer_terminator_test.rs:5:13: 5:16 + // + span: $DIR/derefer_terminator_test.rs:6:13: 6:16 // + literal: Const { ty: fn() -> bool {foo}, val: Value() } } @@ -40,7 +40,7 @@ StorageLive(_2); // scope 1 at $DIR/derefer_terminator_test.rs:+2:9: +2:10 _2 = foo() -> bb2; // scope 1 at $DIR/derefer_terminator_test.rs:+2:13: +2:18 // mir::Constant - // + span: $DIR/derefer_terminator_test.rs:6:13: 6:16 + // + span: $DIR/derefer_terminator_test.rs:7:13: 7:16 // + literal: Const { ty: fn() -> bool {foo}, val: Value() } } diff --git a/src/test/mir-opt/derefer_terminator_test.rs b/src/test/mir-opt/derefer_terminator_test.rs index 787b14ae735d5..d6750c29dd98c 100644 --- a/src/test/mir-opt/derefer_terminator_test.rs +++ b/src/test/mir-opt/derefer_terminator_test.rs @@ -1,3 +1,4 @@ +// unit-test: Derefer // EMIT_MIR derefer_terminator_test.main.Derefer.diff // ignore-wasm32 diff --git a/src/test/mir-opt/derefer_test.rs b/src/test/mir-opt/derefer_test.rs index 2ebc0d343bd74..fad0fe8eb6fc3 100644 --- a/src/test/mir-opt/derefer_test.rs +++ b/src/test/mir-opt/derefer_test.rs @@ -1,3 +1,4 @@ +// unit-test: Derefer // EMIT_MIR derefer_test.main.Derefer.diff fn main() { let mut a = (42,43); diff --git a/src/test/mir-opt/derefer_test_multiple.rs b/src/test/mir-opt/derefer_test_multiple.rs index a27363447fee5..0b3888b07ab45 100644 --- a/src/test/mir-opt/derefer_test_multiple.rs +++ b/src/test/mir-opt/derefer_test_multiple.rs @@ -1,3 +1,4 @@ +// unit-test: Derefer // EMIT_MIR derefer_test_multiple.main.Derefer.diff fn main () { let mut a = (42, 43); diff --git a/src/test/mir-opt/equal_true.rs b/src/test/mir-opt/equal_true.rs index 994cd194a45e2..717d10c6d7627 100644 --- a/src/test/mir-opt/equal_true.rs +++ b/src/test/mir-opt/equal_true.rs @@ -1,3 +1,5 @@ +// unit-test InstCombine + // EMIT_MIR equal_true.opt.InstCombine.diff fn opt(x: bool) -> i32 { diff --git a/src/test/mir-opt/instrument_coverage.rs b/src/test/mir-opt/instrument_coverage.rs index a748f2c5ccc9b..7f6a0a0eb094a 100644 --- a/src/test/mir-opt/instrument_coverage.rs +++ b/src/test/mir-opt/instrument_coverage.rs @@ -1,6 +1,7 @@ // Test that `-C instrument-coverage` injects Coverage statements. The Coverage Counter statements // are later converted into LLVM instrprof.increment intrinsics, during codegen. +// unit-test: InstrumentCoverage // needs-profiler-support // ignore-windows // compile-flags: -C instrument-coverage --remap-path-prefix={{src-base}}=/the/src diff --git a/src/test/mir-opt/not_equal_false.rs b/src/test/mir-opt/not_equal_false.rs index 5fbb848dcb58b..2ae03da40f8a2 100644 --- a/src/test/mir-opt/not_equal_false.rs +++ b/src/test/mir-opt/not_equal_false.rs @@ -1,3 +1,4 @@ +// unit-test: InstCombine // EMIT_MIR not_equal_false.opt.InstCombine.diff fn opt(x: bool) -> u32 { diff --git a/src/test/mir-opt/remove_storage_markers.main.RemoveStorageMarkers.diff b/src/test/mir-opt/remove_storage_markers.main.RemoveStorageMarkers.diff index 750aaa88b1c48..99667aabdaee2 100644 --- a/src/test/mir-opt/remove_storage_markers.main.RemoveStorageMarkers.diff +++ b/src/test/mir-opt/remove_storage_markers.main.RemoveStorageMarkers.diff @@ -23,13 +23,6 @@ scope 3 { debug i => _12; // in scope 3 at $DIR/remove_storage_markers.rs:+2:9: +2:10 } - scope 5 (inlined iter::range::>::next) { // at $DIR/remove_storage_markers.rs:8:14: 8:19 - debug self => _8; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - let mut _14: &mut std::ops::Range; // in scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - } - } - scope 4 (inlined as IntoIterator>::into_iter) { // at $DIR/remove_storage_markers.rs:8:14: 8:19 - debug self => _3; // in scope 4 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL } } @@ -41,29 +34,39 @@ Deinit(_3); // scope 1 at $DIR/remove_storage_markers.rs:+2:14: +2:19 (_3.0: i32) = const 0_i32; // scope 1 at $DIR/remove_storage_markers.rs:+2:14: +2:19 (_3.1: i32) = const 10_i32; // scope 1 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - _2 = move _3; // scope 4 at $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL + _2 = as IntoIterator>::into_iter(move _3) -> bb1; // scope 1 at $DIR/remove_storage_markers.rs:+2:14: +2:19 + // mir::Constant + // + span: $DIR/remove_storage_markers.rs:10:14: 10:19 + // + literal: Const { ty: fn(std::ops::Range) -> as IntoIterator>::IntoIter { as IntoIterator>::into_iter}, val: Value() } + } + + bb1: { - StorageDead(_3); // scope 1 at $DIR/remove_storage_markers.rs:+2:18: +2:19 - StorageLive(_4); // scope 1 at $DIR/remove_storage_markers.rs:+2:14: +2:19 _4 = move _2; // scope 1 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - goto -> bb1; // scope 2 at $DIR/remove_storage_markers.rs:+2:5: +4:6 + goto -> bb2; // scope 2 at $DIR/remove_storage_markers.rs:+2:5: +4:6 } - bb1: { + bb2: { - StorageLive(_6); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - StorageLive(_7); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - StorageLive(_8); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - StorageLive(_9); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 _9 = &mut _4; // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 _8 = &mut (*_9); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 -- StorageLive(_14); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _14 = &mut (*_8); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL - _7 = as iter::range::RangeIteratorImpl>::spec_next(move _14) -> bb4; // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL + _7 = as Iterator>::next(move _8) -> bb3; // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 // mir::Constant - // + span: $SRC_DIR/core/src/iter/range.rs:LL:COL - // + literal: Const { ty: for<'r> fn(&'r mut std::ops::Range) -> Option< as iter::range::RangeIteratorImpl>::Item> { as iter::range::RangeIteratorImpl>::spec_next}, val: Value() } + // + span: $DIR/remove_storage_markers.rs:10:14: 10:19 + // + literal: Const { ty: for<'r> fn(&'r mut std::ops::Range) -> Option< as Iterator>::Item> { as Iterator>::next}, val: Value() } } - bb2: { + bb3: { +- StorageDead(_8); // scope 2 at $DIR/remove_storage_markers.rs:+2:18: +2:19 + _10 = discriminant(_7); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 + switchInt(move _10) -> [0_isize: bb6, 1_isize: bb4, otherwise: bb5]; // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 + } + + bb4: { - StorageLive(_12); // scope 2 at $DIR/remove_storage_markers.rs:+2:9: +2:10 _12 = ((_7 as Some).0: i32); // scope 2 at $DIR/remove_storage_markers.rs:+2:9: +2:10 - StorageLive(_13); // scope 3 at $DIR/remove_storage_markers.rs:+3:16: +3:17 @@ -76,10 +79,14 @@ - StorageDead(_7); // scope 2 at $DIR/remove_storage_markers.rs:+4:5: +4:6 - StorageDead(_6); // scope 2 at $DIR/remove_storage_markers.rs:+4:5: +4:6 _5 = const (); // scope 2 at $DIR/remove_storage_markers.rs:+2:5: +4:6 - goto -> bb1; // scope 2 at $DIR/remove_storage_markers.rs:+2:5: +4:6 + goto -> bb2; // scope 2 at $DIR/remove_storage_markers.rs:+2:5: +4:6 } - bb3: { + bb5: { + unreachable; // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 + } + + bb6: { _0 = const (); // scope 2 at $DIR/remove_storage_markers.rs:+2:5: +4:6 - StorageDead(_9); // scope 2 at $DIR/remove_storage_markers.rs:+4:5: +4:6 - StorageDead(_7); // scope 2 at $DIR/remove_storage_markers.rs:+4:5: +4:6 @@ -89,12 +96,5 @@ - StorageDead(_1); // scope 0 at $DIR/remove_storage_markers.rs:+5:1: +5:2 return; // scope 0 at $DIR/remove_storage_markers.rs:+5:2: +5:2 } - - bb4: { -- StorageDead(_14); // scope 5 at $SRC_DIR/core/src/iter/range.rs:LL:COL -- StorageDead(_8); // scope 2 at $DIR/remove_storage_markers.rs:+2:18: +2:19 - _10 = discriminant(_7); // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - switchInt(move _10) -> [0_isize: bb3, otherwise: bb2]; // scope 2 at $DIR/remove_storage_markers.rs:+2:14: +2:19 - } } diff --git a/src/test/mir-opt/remove_storage_markers.rs b/src/test/mir-opt/remove_storage_markers.rs index c144d3ff79508..f00b826911c6a 100644 --- a/src/test/mir-opt/remove_storage_markers.rs +++ b/src/test/mir-opt/remove_storage_markers.rs @@ -1,3 +1,5 @@ +// unit-test: RemoveStorageMarkers + // Checks that storage markers are removed at opt-level=0. // // compile-flags: -C opt-level=0 -Coverflow-checks=off diff --git a/src/test/mir-opt/remove_zsts_dont_touch_unions.rs b/src/test/mir-opt/remove_zsts_dont_touch_unions.rs index 7a6f86b808509..8b9de9b4d65a6 100644 --- a/src/test/mir-opt/remove_zsts_dont_touch_unions.rs +++ b/src/test/mir-opt/remove_zsts_dont_touch_unions.rs @@ -1,4 +1,4 @@ -// compile-flags: -Zmir-opt-level=3 +// unit-test: RemoveZsts // Ensure RemoveZsts doesn't remove ZST assignments to union fields, // which causes problems in Miri. diff --git a/src/test/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir index 451d0fe4c02dc..05554174ae2c7 100644 --- a/src/test/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir @@ -80,7 +80,7 @@ fn array_casts() -> () { _7 = _2; // scope 3 at $DIR/retag.rs:+3:15: +3:16 _6 = ptr::mut_ptr::::add(move _7, const 1_usize) -> bb1; // scope 3 at $DIR/retag.rs:+3:15: +3:23 // mir::Constant - // + span: $DIR/retag.rs:60:17: 60:20 + // + span: $DIR/retag.rs:61:17: 61:20 // + literal: Const { ty: unsafe fn(*mut usize, usize) -> *mut usize {ptr::mut_ptr::::add}, val: Value() } } @@ -111,7 +111,7 @@ fn array_casts() -> () { _17 = _9; // scope 6 at $DIR/retag.rs:+7:26: +7:27 _16 = ptr::const_ptr::::add(move _17, const 1_usize) -> bb2; // scope 6 at $DIR/retag.rs:+7:26: +7:34 // mir::Constant - // + span: $DIR/retag.rs:64:28: 64:31 + // + span: $DIR/retag.rs:65:28: 65:31 // + literal: Const { ty: unsafe fn(*const usize, usize) -> *const usize {ptr::const_ptr::::add}, val: Value() } } diff --git a/src/test/mir-opt/retag.main.SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/retag.main.SimplifyCfg-elaborate-drops.after.mir index ae6b5cfe21ebb..8802f3b295851 100644 --- a/src/test/mir-opt/retag.main.SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/retag.main.SimplifyCfg-elaborate-drops.after.mir @@ -73,7 +73,7 @@ fn main() -> () { Retag([2phase] _6); // scope 1 at $DIR/retag.rs:+3:29: +3:35 _3 = Test::foo(move _4, move _6) -> [return: bb1, unwind: bb8]; // scope 1 at $DIR/retag.rs:+3:17: +3:36 // mir::Constant - // + span: $DIR/retag.rs:32:25: 32:28 + // + span: $DIR/retag.rs:33:25: 33:28 // + literal: Const { ty: for<'r, 'x> fn(&'r Test, &'x mut i32) -> &'x mut i32 {Test::foo}, val: Value() } } @@ -149,7 +149,7 @@ fn main() -> () { StorageLive(_23); // scope 7 at $DIR/retag.rs:+18:21: +18:23 _28 = const main::promoted[0]; // scope 7 at $DIR/retag.rs:+18:21: +18:23 // mir::Constant - // + span: $DIR/retag.rs:47:21: 47:23 + // + span: $DIR/retag.rs:48:21: 48:23 // + literal: Const { ty: &i32, val: Unevaluated(main, [], Some(promoted[0])) } Retag(_28); // scope 7 at $DIR/retag.rs:+18:21: +18:23 _23 = &(*_28); // scope 7 at $DIR/retag.rs:+18:21: +18:23 @@ -158,7 +158,7 @@ fn main() -> () { Retag(_22); // scope 7 at $DIR/retag.rs:+18:21: +18:23 _19 = Test::foo_shr(move _20, move _22) -> [return: bb4, unwind: bb7]; // scope 7 at $DIR/retag.rs:+18:5: +18:24 // mir::Constant - // + span: $DIR/retag.rs:47:13: 47:20 + // + span: $DIR/retag.rs:48:13: 48:20 // + literal: Const { ty: for<'r, 'x> fn(&'r Test, &'x i32) -> &'x i32 {Test::foo_shr}, val: Value() } } @@ -182,7 +182,7 @@ fn main() -> () { StorageLive(_27); // scope 8 at $DIR/retag.rs:+23:5: +23:18 _27 = array_casts() -> bb6; // scope 8 at $DIR/retag.rs:+23:5: +23:18 // mir::Constant - // + span: $DIR/retag.rs:52:5: 52:16 + // + span: $DIR/retag.rs:53:5: 53:16 // + literal: Const { ty: fn() {array_casts}, val: Value() } } diff --git a/src/test/mir-opt/retag.rs b/src/test/mir-opt/retag.rs index 13568b822d4f3..86deb0e7ccded 100644 --- a/src/test/mir-opt/retag.rs +++ b/src/test/mir-opt/retag.rs @@ -1,3 +1,4 @@ +// unit-test: AddRetag // ignore-wasm32-bare compiled with panic=abort by default // ignore-tidy-linelength // compile-flags: -Z mir-emit-retag -Z mir-opt-level=0 -Z span_free_formats diff --git a/src/test/mir-opt/retag.{impl#0}-foo.SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/retag.{impl#0}-foo.SimplifyCfg-elaborate-drops.after.mir index e395fdb274f37..25d400f0c9fb2 100644 --- a/src/test/mir-opt/retag.{impl#0}-foo.SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/retag.{impl#0}-foo.SimplifyCfg-elaborate-drops.after.mir @@ -1,6 +1,6 @@ -// MIR for `::foo` after SimplifyCfg-elaborate-drops +// MIR for `::foo` after SimplifyCfg-elaborate-drops -fn ::foo(_1: &Test, _2: &mut i32) -> &mut i32 { +fn ::foo(_1: &Test, _2: &mut i32) -> &mut i32 { debug self => _1; // in scope 0 at $DIR/retag.rs:+0:16: +0:21 debug x => _2; // in scope 0 at $DIR/retag.rs:+0:23: +0:24 let mut _0: &mut i32; // return place in scope 0 at $DIR/retag.rs:+0:42: +0:53 diff --git a/src/test/mir-opt/retag.{impl#0}-foo_shr.SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/retag.{impl#0}-foo_shr.SimplifyCfg-elaborate-drops.after.mir index e609166dec892..84ad8afc35760 100644 --- a/src/test/mir-opt/retag.{impl#0}-foo_shr.SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/retag.{impl#0}-foo_shr.SimplifyCfg-elaborate-drops.after.mir @@ -1,6 +1,6 @@ -// MIR for `::foo_shr` after SimplifyCfg-elaborate-drops +// MIR for `::foo_shr` after SimplifyCfg-elaborate-drops -fn ::foo_shr(_1: &Test, _2: &i32) -> &i32 { +fn ::foo_shr(_1: &Test, _2: &i32) -> &i32 { debug self => _1; // in scope 0 at $DIR/retag.rs:+0:20: +0:25 debug x => _2; // in scope 0 at $DIR/retag.rs:+0:27: +0:28 let mut _0: &i32; // return place in scope 0 at $DIR/retag.rs:+0:42: +0:49 diff --git a/src/test/mir-opt/simplify-locals-removes-unused-consts.rs b/src/test/mir-opt/simplify-locals-removes-unused-consts.rs index 179994544723f..39b7911d4aedb 100644 --- a/src/test/mir-opt/simplify-locals-removes-unused-consts.rs +++ b/src/test/mir-opt/simplify-locals-removes-unused-consts.rs @@ -1,3 +1,4 @@ +// unit-test: SimplifyLocals // compile-flags: -C overflow-checks=no fn use_zst(_: ((), ())) {} diff --git a/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff b/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff index da2f6fc440aa2..b41527ba02de5 100644 --- a/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff +++ b/src/test/mir-opt/simplify_locals_removes_unused_consts.main.SimplifyLocals.diff @@ -16,29 +16,53 @@ - let mut _11: Temp; // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 + let _1: (); // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+2:5: +2:22 + let mut _2: ((), ()); // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 -+ let _3: (); // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 ++ let mut _3: (); // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+2:14: +2:16 ++ let mut _4: (); // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+2:18: +2:20 ++ let _5: (); // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 ++ let mut _6: u8; // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:34 ++ let mut _7: u8; // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:30 ++ let mut _8: Temp; // in scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 scope 1 { } bb0: { - StorageLive(_1); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:20: +1:28 - StorageLive(_2); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:21: +1:23 +- Deinit(_2); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:21: +1:23 - StorageLive(_3); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:25: +1:27 +- Deinit(_3); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:25: +1:27 +- Deinit(_1); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:20: +1:28 +- (_1.0: ()) = move _2; // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:20: +1:28 +- (_1.1: ()) = move _3; // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:20: +1:28 - StorageDead(_3); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:27: +1:28 - StorageDead(_2); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:27: +1:28 - StorageDead(_1); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+1:28: +1:29 - StorageLive(_4); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:5: +2:22 - StorageLive(_5); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 - StorageLive(_6); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:14: +2:16 +- Deinit(_6); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:14: +2:16 - StorageLive(_7); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:18: +2:20 +- Deinit(_7); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:18: +2:20 +- Deinit(_5); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 +- (_5.0: ()) = move _6; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 +- (_5.1: ()) = move _7; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 - StorageDead(_7); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:20: +2:21 - StorageDead(_6); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:20: +2:21 - _4 = use_zst(move _5) -> bb1; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:5: +2:22 + StorageLive(_1); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:5: +2:22 + StorageLive(_2); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 ++ StorageLive(_3); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:14: +2:16 ++ Deinit(_3); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:14: +2:16 ++ StorageLive(_4); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:18: +2:20 ++ Deinit(_4); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:18: +2:20 ++ Deinit(_2); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 ++ (_2.0: ()) = move _3; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 ++ (_2.1: ()) = move _4; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:13: +2:21 ++ StorageDead(_4); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:20: +2:21 ++ StorageDead(_3); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:20: +2:21 + _1 = use_zst(move _2) -> bb1; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:5: +2:22 // mir::Constant - // + span: $DIR/simplify-locals-removes-unused-consts.rs:14:5: 14:12 + // + span: $DIR/simplify-locals-removes-unused-consts.rs:15:5: 15:12 // + literal: Const { ty: fn(((), ())) {use_zst}, val: Value() } } @@ -49,22 +73,36 @@ - StorageLive(_9); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:34 - StorageLive(_10); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:30 - StorageLive(_11); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 +- Deinit(_11); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 +- (_11.0: u8) = const 40_u8; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 +- _10 = (_11.0: u8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:30 +- _9 = Add(move _10, const 2_u8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:34 - StorageDead(_10); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:33: +4:34 -- _8 = use_u8(const 42_u8) -> bb2; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 +- _8 = use_u8(move _9) -> bb2; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 + StorageDead(_2); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:21: +2:22 + StorageDead(_1); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+2:22: +2:23 -+ StorageLive(_3); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 -+ _3 = use_u8(const 42_u8) -> bb2; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 ++ StorageLive(_5); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 ++ StorageLive(_6); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:34 ++ StorageLive(_7); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:30 ++ StorageLive(_8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 ++ Deinit(_8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 ++ (_8.0: u8) = const 40_u8; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:28 ++ _7 = (_8.0: u8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:30 ++ _6 = Add(move _7, const 2_u8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:12: +4:34 ++ StorageDead(_7); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:33: +4:34 ++ _5 = use_u8(move _6) -> bb2; // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:5: +4:35 // mir::Constant - // + span: $DIR/simplify-locals-removes-unused-consts.rs:16:5: 16:11 + // + span: $DIR/simplify-locals-removes-unused-consts.rs:17:5: 17:11 // + literal: Const { ty: fn(u8) {use_u8}, val: Value() } } bb2: { - StorageDead(_9); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:34: +4:35 - StorageDead(_11); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:35: +4:36 -- StorageDead(_8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:35: +4:36 -+ StorageDead(_3); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:35: +4:36 ++ StorageDead(_6); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:34: +4:35 + StorageDead(_8); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:35: +4:36 ++ StorageDead(_5); // scope 1 at $DIR/simplify-locals-removes-unused-consts.rs:+4:35: +4:36 + _0 = const (); // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+0:11: +5:2 return; // scope 0 at $DIR/simplify-locals-removes-unused-consts.rs:+5:2: +5:2 } } From bed8e93f40ab77f1dcb2009ff651ec623090769e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 8 Aug 2022 17:25:38 +0200 Subject: [PATCH 10/13] remove Clean trait implementation for hir::ImplItem --- src/librustdoc/clean/inline.rs | 4 +- src/librustdoc/clean/mod.rs | 78 ++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index f644ecb88b931..2b6310870f4f7 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -16,7 +16,7 @@ use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; use crate::clean::{ - self, clean_fn_decl_from_did_and_sig, clean_middle_field, clean_middle_ty, + self, clean_fn_decl_from_did_and_sig, clean_impl_item, clean_middle_field, clean_middle_ty, clean_trait_ref_with_bindings, clean_ty, clean_ty_generics, clean_variant_def, clean_visibility, utils, Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility, }; @@ -416,7 +416,7 @@ pub(crate) fn build_impl( true } }) - .map(|item| item.clean(cx)) + .map(|item| clean_impl_item(item, cx)) .collect::>(), impl_.generics.clean(cx), ), diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index b5244616309dc..fbfafdd280ce4 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1065,45 +1065,46 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext }) } -impl<'tcx> Clean<'tcx, Item> for hir::ImplItem<'tcx> { - fn clean(&self, cx: &mut DocContext<'tcx>) -> Item { - let local_did = self.def_id.to_def_id(); - cx.with_param_env(local_did, |cx| { - let inner = match self.kind { - hir::ImplItemKind::Const(ty, expr) => { - let default = ConstantKind::Local { def_id: local_did, body: expr }; - AssocConstItem(clean_ty(ty, cx), default) - } - hir::ImplItemKind::Fn(ref sig, body) => { - let m = clean_function(cx, sig, self.generics, body); - let defaultness = cx.tcx.impl_defaultness(self.def_id); - MethodItem(m, Some(defaultness)) - } - hir::ImplItemKind::TyAlias(hir_ty) => { - let type_ = clean_ty(hir_ty, cx); - let generics = self.generics.clean(cx); - let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None); - AssocTypeItem( - Box::new(Typedef { type_, generics, item_type: Some(item_type) }), - Vec::new(), - ) - } - }; +pub(crate) fn clean_impl_item<'tcx>( + impl_: &hir::ImplItem<'tcx>, + cx: &mut DocContext<'tcx>, +) -> Item { + let local_did = impl_.def_id.to_def_id(); + cx.with_param_env(local_did, |cx| { + let inner = match impl_.kind { + hir::ImplItemKind::Const(ty, expr) => { + let default = ConstantKind::Local { def_id: local_did, body: expr }; + AssocConstItem(clean_ty(ty, cx), default) + } + hir::ImplItemKind::Fn(ref sig, body) => { + let m = clean_function(cx, sig, impl_.generics, body); + let defaultness = cx.tcx.impl_defaultness(impl_.def_id); + MethodItem(m, Some(defaultness)) + } + hir::ImplItemKind::TyAlias(hir_ty) => { + let type_ = clean_ty(hir_ty, cx); + let generics = impl_.generics.clean(cx); + let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None); + AssocTypeItem( + Box::new(Typedef { type_, generics, item_type: Some(item_type) }), + Vec::new(), + ) + } + }; - let mut what_rustc_thinks = - Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx); + let mut what_rustc_thinks = + Item::from_def_id_and_parts(local_did, Some(impl_.ident.name), inner, cx); - let impl_ref = cx.tcx.impl_trait_ref(cx.tcx.local_parent(self.def_id)); + let impl_ref = cx.tcx.impl_trait_ref(cx.tcx.local_parent(impl_.def_id)); - // Trait impl items always inherit the impl's visibility -- - // we don't want to show `pub`. - if impl_ref.is_some() { - what_rustc_thinks.visibility = Inherited; - } + // Trait impl items always inherit the impl's visibility -- + // we don't want to show `pub`. + if impl_ref.is_some() { + what_rustc_thinks.visibility = Inherited; + } - what_rustc_thinks - }) - } + what_rustc_thinks + }) } impl<'tcx> Clean<'tcx, Item> for ty::AssocItem { @@ -1995,8 +1996,11 @@ fn clean_impl<'tcx>( let tcx = cx.tcx; let mut ret = Vec::new(); let trait_ = impl_.of_trait.as_ref().map(|t| clean_trait_ref(t, cx)); - let items = - impl_.items.iter().map(|ii| tcx.hir().impl_item(ii.id).clean(cx)).collect::>(); + let items = impl_ + .items + .iter() + .map(|ii| clean_impl_item(tcx.hir().impl_item(ii.id), cx)) + .collect::>(); let def_id = tcx.hir().local_def_id(hir_id); // If this impl block is an implementation of the Deref trait, then we From daa0e8fecca0a2e54cb69c3c4ed9518a0960a724 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 8 Aug 2022 17:48:17 +0200 Subject: [PATCH 11/13] remove Clean trait implementation for hir::Generics --- src/librustdoc/clean/inline.rs | 6 +- src/librustdoc/clean/mod.rs | 129 ++++++++++++++++----------------- 2 files changed, 67 insertions(+), 68 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 2b6310870f4f7..58f92eeeb3358 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -16,8 +16,8 @@ use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; use crate::clean::{ - self, clean_fn_decl_from_did_and_sig, clean_impl_item, clean_middle_field, clean_middle_ty, - clean_trait_ref_with_bindings, clean_ty, clean_ty_generics, clean_variant_def, + self, clean_fn_decl_from_did_and_sig, clean_generics, clean_impl_item, clean_middle_field, + clean_middle_ty, clean_trait_ref_with_bindings, clean_ty, clean_ty_generics, clean_variant_def, clean_visibility, utils, Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility, }; use crate::core::DocContext; @@ -418,7 +418,7 @@ pub(crate) fn build_impl( }) .map(|item| clean_impl_item(item, cx)) .collect::>(), - impl_.generics.clean(cx), + clean_generics(impl_.generics, cx), ), None => ( tcx.associated_items(did) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index fbfafdd280ce4..f4f0d3154e1dc 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -575,69 +575,68 @@ fn is_elided_lifetime(param: &hir::GenericParam<'_>) -> bool { matches!(param.kind, hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Elided }) } -impl<'tcx> Clean<'tcx, Generics> for hir::Generics<'tcx> { - fn clean(&self, cx: &mut DocContext<'tcx>) -> Generics { - let impl_trait_params = self - .params - .iter() - .filter(|param| is_impl_trait(param)) - .map(|param| { - let param = clean_generic_param(cx, Some(self), param); - match param.kind { - GenericParamDefKind::Lifetime { .. } => unreachable!(), - GenericParamDefKind::Type { did, ref bounds, .. } => { - cx.impl_trait_bounds.insert(did.into(), bounds.clone()); - } - GenericParamDefKind::Const { .. } => unreachable!(), +pub(crate) fn clean_generics<'tcx>( + gens: &hir::Generics<'tcx>, + cx: &mut DocContext<'tcx>, +) -> Generics { + let impl_trait_params = gens + .params + .iter() + .filter(|param| is_impl_trait(param)) + .map(|param| { + let param = clean_generic_param(cx, Some(gens), param); + match param.kind { + GenericParamDefKind::Lifetime { .. } => unreachable!(), + GenericParamDefKind::Type { did, ref bounds, .. } => { + cx.impl_trait_bounds.insert(did.into(), bounds.clone()); } - param - }) - .collect::>(); + GenericParamDefKind::Const { .. } => unreachable!(), + } + param + }) + .collect::>(); - let mut params = Vec::with_capacity(self.params.len()); - for p in self.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) { - let p = clean_generic_param(cx, Some(self), p); - params.push(p); - } - params.extend(impl_trait_params); + let mut params = Vec::with_capacity(gens.params.len()); + for p in gens.params.iter().filter(|p| !is_impl_trait(p) && !is_elided_lifetime(p)) { + let p = clean_generic_param(cx, Some(gens), p); + params.push(p); + } + params.extend(impl_trait_params); - let mut generics = Generics { - params, - where_predicates: self - .predicates - .iter() - .filter_map(|x| clean_where_predicate(x, cx)) - .collect(), - }; + let mut generics = Generics { + params, + where_predicates: gens + .predicates + .iter() + .filter_map(|x| clean_where_predicate(x, cx)) + .collect(), + }; - // Some duplicates are generated for ?Sized bounds between type params and where - // predicates. The point in here is to move the bounds definitions from type params - // to where predicates when such cases occur. - for where_pred in &mut generics.where_predicates { - match *where_pred { - WherePredicate::BoundPredicate { - ty: Generic(ref name), ref mut bounds, .. - } => { - if bounds.is_empty() { - for param in &mut generics.params { - match param.kind { - GenericParamDefKind::Lifetime { .. } => {} - GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => { - if ¶m.name == name { - mem::swap(bounds, ty_bounds); - break; - } + // Some duplicates are generated for ?Sized bounds between type params and where + // predicates. The point in here is to move the bounds definitions from type params + // to where predicates when such cases occur. + for where_pred in &mut generics.where_predicates { + match *where_pred { + WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds, .. } => { + if bounds.is_empty() { + for param in &mut generics.params { + match param.kind { + GenericParamDefKind::Lifetime { .. } => {} + GenericParamDefKind::Type { bounds: ref mut ty_bounds, .. } => { + if ¶m.name == name { + mem::swap(bounds, ty_bounds); + break; } - GenericParamDefKind::Const { .. } => {} } + GenericParamDefKind::Const { .. } => {} } } } - _ => continue, } + _ => continue, } - generics } + generics } fn clean_ty_generics<'tcx>( @@ -903,7 +902,7 @@ fn clean_function<'tcx>( ) -> Box { let (generics, decl) = enter_impl_trait(cx, |cx| { // NOTE: generics must be cleaned before args - let generics = generics.clean(cx); + let generics = clean_generics(generics, cx); let args = clean_args_from_types_and_body_id(cx, sig.decl.inputs, body_id); let decl = clean_fn_decl_with_args(cx, sig.decl, args); (generics, decl) @@ -1032,7 +1031,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(names)) => { let (generics, decl) = enter_impl_trait(cx, |cx| { // NOTE: generics must be cleaned before args - let generics = trait_item.generics.clean(cx); + let generics = clean_generics(trait_item.generics, cx); let args = clean_args_from_types_and_names(cx, sig.decl.inputs, names); let decl = clean_fn_decl_with_args(cx, sig.decl, args); (generics, decl) @@ -1040,7 +1039,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext TyMethodItem(Box::new(Function { decl, generics })) } hir::TraitItemKind::Type(bounds, Some(default)) => { - let generics = enter_impl_trait(cx, |cx| trait_item.generics.clean(cx)); + let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)); let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(); let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, default), cx, None); AssocTypeItem( @@ -1053,7 +1052,7 @@ fn clean_trait_item<'tcx>(trait_item: &hir::TraitItem<'tcx>, cx: &mut DocContext ) } hir::TraitItemKind::Type(bounds, None) => { - let generics = enter_impl_trait(cx, |cx| trait_item.generics.clean(cx)); + let generics = enter_impl_trait(cx, |cx| clean_generics(trait_item.generics, cx)); let bounds = bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(); TyAssocTypeItem(Box::new(generics), bounds) } @@ -1083,7 +1082,7 @@ pub(crate) fn clean_impl_item<'tcx>( } hir::ImplItemKind::TyAlias(hir_ty) => { let type_ = clean_ty(hir_ty, cx); - let generics = impl_.generics.clean(cx); + let generics = clean_generics(impl_.generics, cx); let item_type = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None); AssocTypeItem( Box::new(Typedef { type_, generics, item_type: Some(item_type) }), @@ -1913,32 +1912,32 @@ fn clean_maybe_renamed_item<'tcx>( }), ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy { bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(), - generics: ty.generics.clean(cx), + generics: clean_generics(ty.generics, cx), }), ItemKind::TyAlias(hir_ty, generics) => { let rustdoc_ty = clean_ty(hir_ty, cx); let ty = clean_middle_ty(hir_ty_to_ty(cx.tcx, hir_ty), cx, None); TypedefItem(Box::new(Typedef { type_: rustdoc_ty, - generics: generics.clean(cx), + generics: clean_generics(generics, cx), item_type: Some(ty), })) } ItemKind::Enum(ref def, generics) => EnumItem(Enum { variants: def.variants.iter().map(|v| v.clean(cx)).collect(), - generics: generics.clean(cx), + generics: clean_generics(generics, cx), }), ItemKind::TraitAlias(generics, bounds) => TraitAliasItem(TraitAlias { - generics: generics.clean(cx), + generics: clean_generics(generics, cx), bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(), }), ItemKind::Union(ref variant_data, generics) => UnionItem(Union { - generics: generics.clean(cx), + generics: clean_generics(generics, cx), fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(), }), ItemKind::Struct(ref variant_data, generics) => StructItem(Struct { struct_type: CtorKind::from_hir(variant_data), - generics: generics.clean(cx), + generics: clean_generics(generics, cx), fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(), }), ItemKind::Impl(impl_) => return clean_impl(impl_, item.hir_id(), cx), @@ -1961,7 +1960,7 @@ fn clean_maybe_renamed_item<'tcx>( TraitItem(Trait { def_id, items, - generics: generics.clean(cx), + generics: clean_generics(generics, cx), bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(), }) } @@ -2017,7 +2016,7 @@ fn clean_impl<'tcx>( let mut make_item = |trait_: Option, for_: Type, items: Vec| { let kind = ImplItem(Box::new(Impl { unsafety: impl_.unsafety, - generics: impl_.generics.clean(cx), + generics: clean_generics(impl_.generics, cx), trait_, for_, items, @@ -2212,7 +2211,7 @@ fn clean_maybe_renamed_foreign_item<'tcx>( hir::ForeignItemKind::Fn(decl, names, generics) => { let (generics, decl) = enter_impl_trait(cx, |cx| { // NOTE: generics must be cleaned before args - let generics = generics.clean(cx); + let generics = clean_generics(generics, cx); let args = clean_args_from_types_and_names(cx, decl.inputs, names); let decl = clean_fn_decl_with_args(cx, decl, args); (generics, decl) From fb8636fc4826dbab5a728cb75bff0e9295c8e59a Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sun, 7 Aug 2022 22:41:40 -0700 Subject: [PATCH 12/13] Set tainted errors bit before emitting coerce suggestions. --- compiler/rustc_typeck/src/check/coercion.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs index 21d0a7869d68e..8ab9f77d0ea78 100644 --- a/compiler/rustc_typeck/src/check/coercion.rs +++ b/compiler/rustc_typeck/src/check/coercion.rs @@ -1479,6 +1479,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { } } Err(coercion_error) => { + // Mark that we've failed to coerce the types here to suppress + // any superfluous errors we might encounter while trying to + // emit or provide suggestions on how to fix the initial error. + fcx.set_tainted_by_errors(); let (expected, found) = if label_expression_as_expected { // In the case where this is a "forced unit", like // `break`, we want to call the `()` "expected" From 75cc9cddf7ebcbf00213bbdf6a9e2c78bc0876f1 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Mon, 8 Aug 2022 00:32:35 -0700 Subject: [PATCH 13/13] Add test for #100246. --- src/test/ui/typeck/issue-100246.rs | 30 ++++++++++++++++++++++++++ src/test/ui/typeck/issue-100246.stderr | 13 +++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/test/ui/typeck/issue-100246.rs create mode 100644 src/test/ui/typeck/issue-100246.stderr diff --git a/src/test/ui/typeck/issue-100246.rs b/src/test/ui/typeck/issue-100246.rs new file mode 100644 index 0000000000000..8f0b34bab0c87 --- /dev/null +++ b/src/test/ui/typeck/issue-100246.rs @@ -0,0 +1,30 @@ +#![recursion_limit = "5"] // To reduce noise + +//expect incompatible type error when ambiguous traits are in scope +//and not an overflow error on the span in the main function. + +struct Ratio(T); + +pub trait Pow { + fn pow(self) -> Self; +} + +impl<'a, T> Pow for &'a Ratio +where + &'a T: Pow, +{ + fn pow(self) -> Self { + self + } +} + +fn downcast<'a, W: ?Sized>() -> std::io::Result<&'a W> { + todo!() +} + +struct Other; + +fn main() -> std::io::Result<()> { + let other: Other = downcast()?;//~ERROR 28:24: 28:35: `?` operator has incompatible types + Ok(()) +} diff --git a/src/test/ui/typeck/issue-100246.stderr b/src/test/ui/typeck/issue-100246.stderr new file mode 100644 index 0000000000000..8b77de94e89f8 --- /dev/null +++ b/src/test/ui/typeck/issue-100246.stderr @@ -0,0 +1,13 @@ +error[E0308]: `?` operator has incompatible types + --> $DIR/issue-100246.rs:28:24 + | +LL | let other: Other = downcast()?; + | ^^^^^^^^^^^ expected struct `Other`, found reference + | + = note: `?` operator cannot convert from `&_` to `Other` + = note: expected struct `Other` + found reference `&_` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.