From f7035834758a994278a07ef7ec2e702e644363e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 30 Mar 2024 01:10:39 +0000 Subject: [PATCH] Do not attempt to access HIR parent of Synthetic node Fix #122991. Follow up to #122158. --- compiler/rustc_middle/src/ty/context.rs | 12 ++++-- ...ic-by-accessing-parent-hir-of-synthetic.rs | 12 ++++++ ...y-accessing-parent-hir-of-synthetic.stderr | 37 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.rs create mode 100644 tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.stderr diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 188cb50849dc1..26b4e6cea3c3f 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2251,9 +2251,15 @@ impl<'tcx> TyCtxt<'tcx> { /// Find the crate root and the appropriate span where `use` and outer attributes can be /// inserted at. pub fn crate_level_attribute_injection_span(self, hir_id: HirId) -> Option { - for (_hir_id, node) in self.hir().parent_iter(hir_id) { - if let hir::Node::Crate(m) = node { - return Some(m.spans.inject_use_span.shrink_to_lo()); + for (_hir_id, node) in + [(hir_id, self.hir_node(hir_id))].into_iter().chain(self.hir().parent_iter(hir_id)) + { + match node { + hir::Node::Synthetic => return None, + hir::Node::Crate(m) => { + return Some(m.spans.inject_use_span.shrink_to_lo()); + } + _ => {} } } None diff --git a/tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.rs b/tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.rs new file mode 100644 index 0000000000000..f51e1d1379eff --- /dev/null +++ b/tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.rs @@ -0,0 +1,12 @@ +pub trait Foo<'a> { + type Assoc; + + fn demo() -> impl Foo + //~^ ERROR missing lifetime specifier + //~| ERROR the trait bound `String: Copy` is not satisfied + where + String: Copy; + //~^ ERROR the trait bound `String: Copy` is not satisfied +} + +fn main() {} diff --git a/tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.stderr b/tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.stderr new file mode 100644 index 0000000000000..0f3037096808c --- /dev/null +++ b/tests/ui/suggestions/dont-panic-by-accessing-parent-hir-of-synthetic.stderr @@ -0,0 +1,37 @@ +error[E0106]: missing lifetime specifier + --> $DIR/dont-panic-by-accessing-parent-hir-of-synthetic.rs:4:23 + | +LL | fn demo() -> impl Foo + | ^^^ expected named lifetime parameter + | + = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from +help: consider using the `'a` lifetime + | +LL | fn demo() -> impl Foo<'a> + | ++++ + +error[E0277]: the trait bound `String: Copy` is not satisfied + --> $DIR/dont-panic-by-accessing-parent-hir-of-synthetic.rs:8:9 + | +LL | String: Copy; + | ^^^^^^^^^^^^ the trait `Copy` is not implemented for `String` + | + = help: see issue #48214 +help: add `#![feature(trivial_bounds)]` to the crate attributes to enable + | +LL + #![feature(trivial_bounds)] + | + +error[E0277]: the trait bound `String: Copy` is not satisfied + --> $DIR/dont-panic-by-accessing-parent-hir-of-synthetic.rs:4:18 + | +LL | fn demo() -> impl Foo + | ^^^^^^^^ the trait `Copy` is not implemented for `String` + | + = help: see issue #48214 + = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0106, E0277. +For more information about an error, try `rustc --explain E0106`.