Skip to content

Commit

Permalink
Do not attempt to access HIR parent of Synthetic node
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Mar 30, 2024
1 parent 399fa2f commit c2663b7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].
pub fn parent_hir_id(self, hir_id: HirId) -> HirId {
let HirId { owner, local_id } = hir_id;
info!(?owner, ?local_id);
if local_id == ItemLocalId::from_u32(0) {
self.hir_owner_parent(owner)
} else {
Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Span> {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit c2663b7

Please sign in to comment.