Skip to content

Commit f703583

Browse files
committed
Do not attempt to access HIR parent of Synthetic node
Fix rust-lang#122991. Follow up to rust-lang#122158.
1 parent 399fa2f commit f703583

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

compiler/rustc_middle/src/ty/context.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2251,9 +2251,15 @@ impl<'tcx> TyCtxt<'tcx> {
22512251
/// Find the crate root and the appropriate span where `use` and outer attributes can be
22522252
/// inserted at.
22532253
pub fn crate_level_attribute_injection_span(self, hir_id: HirId) -> Option<Span> {
2254-
for (_hir_id, node) in self.hir().parent_iter(hir_id) {
2255-
if let hir::Node::Crate(m) = node {
2256-
return Some(m.spans.inject_use_span.shrink_to_lo());
2254+
for (_hir_id, node) in
2255+
[(hir_id, self.hir_node(hir_id))].into_iter().chain(self.hir().parent_iter(hir_id))
2256+
{
2257+
match node {
2258+
hir::Node::Synthetic => return None,
2259+
hir::Node::Crate(m) => {
2260+
return Some(m.spans.inject_use_span.shrink_to_lo());
2261+
}
2262+
_ => {}
22572263
}
22582264
}
22592265
None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub trait Foo<'a> {
2+
type Assoc;
3+
4+
fn demo() -> impl Foo
5+
//~^ ERROR missing lifetime specifier
6+
//~| ERROR the trait bound `String: Copy` is not satisfied
7+
where
8+
String: Copy;
9+
//~^ ERROR the trait bound `String: Copy` is not satisfied
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0106]: missing lifetime specifier
2+
--> $DIR/dont-panic-by-accessing-parent-hir-of-synthetic.rs:4:23
3+
|
4+
LL | fn demo() -> impl Foo
5+
| ^^^ expected named lifetime parameter
6+
|
7+
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
8+
help: consider using the `'a` lifetime
9+
|
10+
LL | fn demo() -> impl Foo<'a>
11+
| ++++
12+
13+
error[E0277]: the trait bound `String: Copy` is not satisfied
14+
--> $DIR/dont-panic-by-accessing-parent-hir-of-synthetic.rs:8:9
15+
|
16+
LL | String: Copy;
17+
| ^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
18+
|
19+
= help: see issue #48214
20+
help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
21+
|
22+
LL + #![feature(trivial_bounds)]
23+
|
24+
25+
error[E0277]: the trait bound `String: Copy` is not satisfied
26+
--> $DIR/dont-panic-by-accessing-parent-hir-of-synthetic.rs:4:18
27+
|
28+
LL | fn demo() -> impl Foo
29+
| ^^^^^^^^ the trait `Copy` is not implemented for `String`
30+
|
31+
= help: see issue #48214
32+
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable
33+
34+
error: aborting due to 3 previous errors
35+
36+
Some errors have detailed explanations: E0106, E0277.
37+
For more information about an error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)