Skip to content

Commit

Permalink
Implement necessary checks for tail calls
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Jun 15, 2023
1 parent ced6c93 commit 20c26dc
Show file tree
Hide file tree
Showing 22 changed files with 874 additions and 24 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,12 @@ rustc_queries! {
cache_on_disk_if { true }
}

/// Checks well-formedness of tail calls (`become f()`).
query thir_check_tail_calls(key: LocalDefId) -> Result<(), rustc_errors::ErrorGuaranteed> {
desc { |tcx| "tail-call-checking `{}`", tcx.def_path_str(key) }
cache_on_disk_if { true }
}

/// Returns the types assumed to be well formed while "inside" of the given item.
///
/// Note that we've liberated the late bound regions of function signatures, so
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
),
ExprKind::Become { value } => {
let v = &this.thir[value];
let ExprKind::Scope { region_scope, lint_level, value, .. } = v.kind else { span_bug!(v.span, "what {v:?}") };
let ExprKind::Scope { region_scope, lint_level, value, .. } = v.kind
else { span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}") };
let v = &this.thir[value];
let ExprKind::Call { ref args, fun, fn_span, .. } = v.kind else { span_bug!(v.span, "what {v:?}") };
let ExprKind::Call { ref args, fun, fn_span, .. } = v.kind
else { span_bug!(v.span, "`thir_check_tail_calls` should have disallowed this {v:?}") };

this.in_scope((region_scope, source_info), lint_level, |this| {
let fun = unpack!(block = this.as_local_operand(block, &this.thir[fun]));
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ fn mir_build(tcx: TyCtxt<'_>, def: LocalDefId) -> Body<'_> {
return construct_error(tcx, def, e);
}

if let Err(err) = tcx.thir_check_tail_calls(def) {
return construct_error(tcx, def, err);
}

let body = match tcx.thir_body(def) {
Err(error_reported) => construct_error(tcx, def, error_reported),
Ok((thir, expr)) => {
Expand Down
Loading

0 comments on commit 20c26dc

Please sign in to comment.