Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not attempt to access HIR parent of Synthetic node #123215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

@compiler-errors compiler-errors Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather we not fix this in the parent_iter caller given that it's a bit specific solution to a general problem.

I think we should fix this in TyCtxtFeed::feed_hir -- I'll open a PR, but I'd rather get @petrochenkov to review it as the author of this Node::Synthetic stuff who might have opinions on the API.

edit: #123218

{
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`.
Loading