Skip to content
Merged
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
40 changes: 37 additions & 3 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let generics = self.lower_delegation_generics(span);
DelegationResults { body_id, sig, ident, generics }
}
Err(err) => self.generate_delegation_error(err, span),
Err(err) => self.generate_delegation_error(err, span, delegation),
}
}

Expand Down Expand Up @@ -404,6 +404,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&mut self,
err: ErrorGuaranteed,
span: Span,
delegation: &Delegation,
) -> DelegationResults<'hir> {
let generics = self.lower_delegation_generics(span);

Expand All @@ -418,8 +419,41 @@ impl<'hir> LoweringContext<'_, 'hir> {
let header = self.generate_header_error();
let sig = hir::FnSig { decl, header, span };

let ident = Ident::dummy();
let body_id = self.lower_body(|this| (&[], this.mk_expr(hir::ExprKind::Err(err), span)));
let ident = self.lower_ident(delegation.ident);

let body_id = self.lower_body(|this| {
let body_expr = match delegation.body.as_ref() {
Some(box block) => {
// Generates a block when we failed to resolve delegation, where a target expression is its only statement,
// thus there will be no ICEs on further stages of analysis (see #144594)

// As we generate a void function we want to convert target expression to statement to avoid additional
// errors, such as mismatched return type
let stmts = this.arena.alloc_from_iter([hir::Stmt {
hir_id: this.next_id(),
kind: rustc_hir::StmtKind::Semi(
this.arena.alloc(this.lower_target_expr(block)),
),
span,
}]);

let block = this.arena.alloc(hir::Block {
stmts,
expr: None,
hir_id: this.next_id(),
rules: hir::BlockCheckMode::DefaultBlock,
span,
targeted_by_break: false,
});

hir::ExprKind::Block(block, None)
}
None => hir::ExprKind::Err(err),
};

(&[], this.mk_expr(body_expr, span))
});

DelegationResults { ident, generics, body_id, sig }
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/delegation/ice-line-bounds-issue-148732.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ reuse a as b {
//~| ERROR functions delegation is not yet fully implemented
dbg!(b);
//~^ ERROR missing lifetime specifier
//~| ERROR `fn() {b}` doesn't implement `Debug`
}

fn main() {}
18 changes: 15 additions & 3 deletions tests/ui/delegation/ice-line-bounds-issue-148732.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ LL | / reuse a as b {
LL | |
LL | |
LL | | dbg!(b);
LL | |
... |
LL | | }
| |_^
|
= note: see issue #118212 <https://github.com/rust-lang/rust/issues/118212> for more information
= help: add `#![feature(fn_delegation)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 3 previous errors
error[E0277]: `fn() {b}` doesn't implement `Debug`
--> $DIR/ice-line-bounds-issue-148732.rs:4:5
|
LL | reuse a as b {
| - consider calling this function
...
LL | dbg!(b);
| ^^^^^^^ the trait `Debug` is not implemented for fn item `fn() {b}`
|
= help: use parentheses to call this function: `b()`
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0106, E0425, E0658.
Some errors have detailed explanations: E0106, E0277, E0425, E0658.
For more information about an error, try `rustc --explain E0106`.
13 changes: 13 additions & 0 deletions tests/ui/delegation/unused-import-ice-144594.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(incomplete_features)]
#![feature(fn_delegation)]

reuse a as b {
//~^ ERROR cannot find function `a` in this scope [E0425]
|| {
use std::ops::Add;
x.add
//~^ ERROR cannot find value `x` in this scope [E0425]
}
}

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/delegation/unused-import-ice-144594.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0425]: cannot find function `a` in this scope
--> $DIR/unused-import-ice-144594.rs:4:7
|
LL | reuse a as b {
| ^ not found in this scope

error[E0425]: cannot find value `x` in this scope
--> $DIR/unused-import-ice-144594.rs:8:9
|
LL | x.add
| ^ not found in this scope

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.
Loading