-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Create const blocks on the fly during typeck instead of waiting until writeback. #125806
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -335,7 +335,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||||
} | ||||||||
ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected), | ||||||||
ExprKind::Array(args) => self.check_expr_array(args, expected, expr), | ||||||||
ExprKind::ConstBlock(ref block) => self.check_expr_with_expectation(block, expected), | ||||||||
ExprKind::ConstBlock(ref block) => { | ||||||||
self.check_expr_const_block(block, expr.hir_id, expr.span, expected) | ||||||||
} | ||||||||
ExprKind::Repeat(element, ref count) => { | ||||||||
self.check_expr_repeat(element, count, expected, expr) | ||||||||
} | ||||||||
|
@@ -903,15 +905,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||||
// We are inside a function body, so reporting "return statement | ||||||||
// outside of function body" needs an explanation. | ||||||||
|
||||||||
let encl_body_owner_id = self.tcx.hir().enclosing_body_owner(expr.hir_id); | ||||||||
|
||||||||
// If this didn't hold, we would not have to report an error in | ||||||||
// the first place. | ||||||||
assert_ne!(encl_item_id.def_id, encl_body_owner_id); | ||||||||
assert_ne!(encl_item_id.def_id, self.body_id); | ||||||||
|
||||||||
let encl_body = self.tcx.hir().body_owned_by(encl_body_owner_id); | ||||||||
|
||||||||
err.encl_body_span = Some(encl_body.value.span); | ||||||||
err.encl_body_span = Some(self.tcx.def_span(self.body_id)); | ||||||||
Comment on lines
-906
to
+912
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
err.encl_fn_span = Some(*encl_fn_span); | ||||||||
} | ||||||||
|
||||||||
|
@@ -1458,6 +1456,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
fn check_expr_const_block( | ||||||||
&self, | ||||||||
block: &'tcx hir::Expr<'tcx>, | ||||||||
hir_id: HirId, | ||||||||
span: Span, | ||||||||
expected: Expectation<'tcx>, | ||||||||
) -> Ty<'tcx> { | ||||||||
let feed = self.tcx().create_def(self.body_id, kw::Empty, DefKind::InlineConst); | ||||||||
feed.def_span(span); | ||||||||
feed.local_def_id_to_hir_id(hir_id); | ||||||||
Comment on lines
+1466
to
+1468
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like const blocks should be first-class nested bodies (liek closures) all the way from the AST-level (i.e. having a def id, being treated as a hir owner, etc) instead of having to synthesize a def-id here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps yeah There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. though we'll def need to lazily create the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now we are breaking query system invariants and are feeding anon const There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why do we need to feed the The rust/compiler/rustc_hir_analysis/src/collect/type_of.rs Lines 483 to 485 in 2a2c29a
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't understand why this needs to be entangled with inline const exprs. Perhaps we should just distinguish these two rather than treating them the same. |
||||||||
self.typeck_results.borrow_mut().inline_consts.insert(hir_id.local_id, feed.def_id()); | ||||||||
|
||||||||
// Create a new function context. | ||||||||
let fcx = FnCtxt::new(self, self.param_env, feed.key()); | ||||||||
|
||||||||
let ty = fcx.check_expr_with_expectation(block, expected); | ||||||||
fcx.require_type_is_sized(ty, block.span, ObligationCauseCode::ConstSized); | ||||||||
fcx.write_ty(block.hir_id, ty); | ||||||||
ty | ||||||||
} | ||||||||
|
||||||||
fn check_expr_repeat( | ||||||||
&self, | ||||||||
element: &'tcx hir::Expr<'tcx>, | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//@edition:2021 | ||
|
||
fn foo() { | ||
const { return } | ||
//~^ ERROR: return statement outside of function body | ||
} | ||
|
||
fn labelled_block_break() { | ||
'a: { const { break 'a } } | ||
//~^ ERROR: `break` outside of a loop or labeled block | ||
//~| ERROR: use of unreachable label | ||
} | ||
|
||
fn loop_break() { | ||
loop { | ||
const { break } | ||
//~^ ERROR: `break` outside of a loop or labeled block | ||
} | ||
} | ||
|
||
fn continue_to_labelled_block() { | ||
'a: { const { continue 'a } } | ||
//~^ ERROR: `continue` outside of a loop | ||
//~| ERROR: use of unreachable label | ||
} | ||
|
||
fn loop_continue() { | ||
loop { | ||
const { continue } | ||
//~^ ERROR: `continue` outside of a loop | ||
} | ||
} | ||
|
||
async fn await_across_const_block() { | ||
const { async {}.await } | ||
//~^ ERROR: `await` is only allowed inside `async` functions and blocks | ||
} | ||
|
||
fn main() {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
error[E0767]: use of unreachable label `'a` | ||
--> $DIR/cross_const_control_flow.rs:9:25 | ||
| | ||
LL | 'a: { const { break 'a } } | ||
| -- ^^ unreachable label `'a` | ||
| | | ||
| unreachable label defined here | ||
| | ||
= note: labels are unreachable through functions, closures, async blocks and modules | ||
|
||
error[E0767]: use of unreachable label `'a` | ||
--> $DIR/cross_const_control_flow.rs:22:28 | ||
| | ||
LL | 'a: { const { continue 'a } } | ||
| -- ^^ unreachable label `'a` | ||
| | | ||
| unreachable label defined here | ||
| | ||
= note: labels are unreachable through functions, closures, async blocks and modules | ||
|
||
error[E0728]: `await` is only allowed inside `async` functions and blocks | ||
--> $DIR/cross_const_control_flow.rs:35:22 | ||
| | ||
LL | const { async {}.await } | ||
| -----------------^^^^^-- | ||
| | | | ||
| | only allowed inside `async` functions and blocks | ||
| this is not `async` | ||
|
||
error[E0268]: `break` outside of a loop or labeled block | ||
--> $DIR/cross_const_control_flow.rs:9:19 | ||
| | ||
LL | 'a: { const { break 'a } } | ||
| ^^^^^^^^ cannot `break` outside of a loop or labeled block | ||
|
||
error[E0268]: `break` outside of a loop or labeled block | ||
--> $DIR/cross_const_control_flow.rs:16:17 | ||
| | ||
LL | const { break } | ||
| ^^^^^ cannot `break` outside of a loop or labeled block | ||
|
||
error[E0268]: `continue` outside of a loop | ||
--> $DIR/cross_const_control_flow.rs:22:19 | ||
| | ||
LL | 'a: { const { continue 'a } } | ||
| ^^^^^^^^^^^ cannot `continue` outside of a loop | ||
|
||
error[E0268]: `continue` outside of a loop | ||
--> $DIR/cross_const_control_flow.rs:29:17 | ||
| | ||
LL | const { continue } | ||
| ^^^^^^^^ cannot `continue` outside of a loop | ||
|
||
error[E0572]: return statement outside of function body | ||
--> $DIR/cross_const_control_flow.rs:4:13 | ||
| | ||
LL | / fn foo() { | ||
LL | | const { return } | ||
| | --------^^^^^^-- the return is part of this body... | ||
LL | | | ||
LL | | } | ||
| |_- ...not the enclosing function body | ||
|
||
error: aborting due to 8 previous errors | ||
|
||
Some errors have detailed explanations: E0268, E0572, E0728, E0767. | ||
For more information about an error, try `rustc --explain E0268`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of strange that
coroutine_kind
is not handled inwith_new_scopes
. Is there some otherwith_body_*
-like function that does the coroutine kind handling too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been looking into this in a branch, but didn't want to add more stuff to this PR.
we can get it all working by merging
with_new_scopes
intolower_body
, but it needs some managing of the right spans to maintain diagnostic quality.