Skip to content

Commit

Permalink
Auto merge of rust-lang#123531 - compiler-errors:closure-wf, r=
Browse files Browse the repository at this point in the history
Enforce closure args + return type are WF

I found this out when investigating rust-lang#123461 (comment). Turns out we don't register WF obligations for closure args and return types, leading to the ICE.

I think this is a useful thing to check for, but I'd like to check what the fallout is.

Worst case, I think we should enforce this across an edition boundary (and possibly eventually migrate this for all editions).
  • Loading branch information
bors committed Apr 6, 2024
2 parents 30840c5 + 04b9db9 commit 18fb860
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 39 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ struct DiagCtxtInner {
}

/// A key denoting where from a diagnostic was stashed.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum StashKey {
ItemNoType,
UnderscoreForArrayLengths,
Expand Down Expand Up @@ -780,7 +780,7 @@ impl DiagCtxt {
// We delay a bug here so that `-Ztreat-err-as-bug -Zeagerly-emit-delayed-bugs`
// can be used to create a backtrace at the stashing site insted of whenever the
// diagnostic context is dropped and thus delayed bugs are emitted.
Error => Some(self.span_delayed_bug(span, "stashing {key:?}")),
Error => Some(self.span_delayed_bug(span, format!("stashing {key:?}"))),
DelayedBug => return self.inner.borrow_mut().emit_diagnostic(diag),
ForceWarning(_) | Warning | Note | OnceNote | Help | OnceHelp | FailureNote | Allow
| Expect(_) => None,
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir_analysis::check::{check_function_signature, forbid_intrinsic_abi};
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::RegionVariableOrigin;
use rustc_infer::traits::WellFormedLoc;
use rustc_middle::ty::{self, Binder, Ty, TyCtxt};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::sym;
Expand Down Expand Up @@ -71,6 +72,18 @@ pub(super) fn check_fn<'a, 'tcx>(
let inputs_hir = hir.fn_decl_by_hir_id(fn_id).map(|decl| &decl.inputs);
let inputs_fn = fn_sig.inputs().iter().copied();
for (idx, (param_ty, param)) in inputs_fn.chain(maybe_va_list).zip(body.params).enumerate() {
// We checked the root's signature during wfcheck, but not the child.
if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
fcx.register_wf_obligation(
param_ty.into(),
param.span,
traits::WellFormed(Some(WellFormedLoc::Param {
function: fn_def_id,
param_idx: idx,
})),
);
}

// Check the pattern.
let ty: Option<&hir::Ty<'_>> = inputs_hir.and_then(|h| h.get(idx));
let ty_span = ty.map(|ty| ty.span);
Expand Down Expand Up @@ -108,7 +121,13 @@ pub(super) fn check_fn<'a, 'tcx>(
hir::FnRetTy::DefaultReturn(_) => body.value.span,
hir::FnRetTy::Return(ty) => ty.span,
};

fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::SizedReturnType);
// We checked the root's signature during wfcheck, but not the child.
if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::WellFormed(None));
}

fcx.is_whole_body.set(true);
fcx.check_return_expr(body.value, false);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ pub enum WellFormedLoc {
/// The index of the parameter to use.
/// Parameters are indexed from 0, with the return type
/// being the last 'parameter'
param_idx: u16,
param_idx: usize,
},
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ui/inference/issue-80409.compat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0277]: the trait bound `usize: Fsm` is not satisfied
--> $DIR/issue-80409.rs:36:31
|
LL | builder.state().on_entry(|_| {});
| ^ the trait `Fsm` is not implemented for `usize`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-80409.rs:26:1
|
LL | trait Fsm {
| ^^^^^^^^^
note: required by a bound in `StateContext`
--> $DIR/issue-80409.rs:30:31
|
LL | struct StateContext<'a, TFsm: Fsm> {
| ^^^ required by this bound in `StateContext`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
24 changes: 15 additions & 9 deletions tests/ui/inference/issue-80409.no-compat.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
error: internal compiler error: error performing ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: ImpliedOutlivesBounds { ty: &'?2 mut StateContext<'?3, usize> } }
--> $DIR/issue-80409.rs:49:30
error[E0277]: the trait bound `usize: Fsm` is not satisfied
--> $DIR/issue-80409.rs:36:31
|
LL | builder.state().on_entry(|_| {});
| ^^^
| ^ the trait `Fsm` is not implemented for `usize`
|
note:
--> $DIR/issue-80409.rs:49:30
help: this trait has no implementations, consider adding one
--> $DIR/issue-80409.rs:26:1
|
LL | builder.state().on_entry(|_| {});
| ^^^
LL | trait Fsm {
| ^^^^^^^^^
note: required by a bound in `StateContext`
--> $DIR/issue-80409.rs:30:31
|
LL | struct StateContext<'a, TFsm: Fsm> {
| ^^^ required by this bound in `StateContext`

error: aborting due to 1 previous error

query stack during panic:
end of query stack
For more information about this error, try `rustc --explain E0277`.
14 changes: 1 addition & 13 deletions tests/ui/inference/issue-80409.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
// This should not pass, because `usize: Fsm` does not hold. However, it currently ICEs.

// ignore-tidy-linelength

//@ revisions: compat no-compat
//@[compat] check-pass
//@[no-compat] compile-flags: -Zno-implied-bounds-compat
//@[no-compat] check-fail
//@[no-compat] known-bug: #80409
//@[no-compat] failure-status: 101
//@[no-compat] normalize-stderr-test "delayed at.*" -> ""
//@[no-compat] normalize-stderr-test "note: .*\n\n" -> ""
//@[no-compat] normalize-stderr-test "thread 'rustc' panicked.*\n" -> ""
//@[no-compat] normalize-stderr-test "(error: internal compiler error: [^:]+):\d+:\d+: " -> "$1:LL:CC: "
//@[no-compat] rustc-env:RUST_BACKTRACE=0

#![allow(unreachable_code, unused)]

Expand Down Expand Up @@ -47,4 +34,5 @@ struct StateContext<'a, TFsm: Fsm> {
fn main() {
let mut builder: FsmBuilder<usize> = todo!();
builder.state().on_entry(|_| {});
//~^ ERROR the trait bound `usize: Fsm` is not satisfied
}
1 change: 1 addition & 0 deletions tests/ui/issues/issue-66706.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn b() {
fn c() {
[0; [|&_: _ &_| {}; 0 ].len()]
//~^ ERROR expected `,`, found `&`
//~| ERROR type annotations needed
}

fn d() {
Expand Down
10 changes: 8 additions & 2 deletions tests/ui/issues/issue-66706.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LL | [0; [|&_: _ &_| {}; 0 ].len()]
| help: missing `,`

error: expected identifier, found reserved identifier `_`
--> $DIR/issue-66706.rs:18:26
--> $DIR/issue-66706.rs:19:26
|
LL | [0; match [|f @ &ref _| () ] {} ]
| ----- ^ expected identifier, found reserved identifier
Expand All @@ -34,6 +34,12 @@ error[E0282]: type annotations needed
LL | [0; [|_: _ &_| ()].len()]
| ^ cannot infer type

error: aborting due to 5 previous errors
error[E0282]: type annotations needed
--> $DIR/issue-66706.rs:13:11
|
LL | [0; [|&_: _ &_| {}; 0 ].len()]
| ^^^^^ cannot infer type

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0282`.
4 changes: 2 additions & 2 deletions tests/ui/traits/mutual-recursion-issue-75860.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ pub fn iso<A, B, F1, F2>(a: F1, b: F2) -> (Box<dyn Fn(A) -> B>, Box<dyn Fn(B) ->
(Box::new(a), Box::new(b))
}
pub fn iso_un_option<A, B>() -> (Box<dyn Fn(A) -> B>, Box<dyn Fn(B) -> A>) {
let left = |o_a: Option<_>| o_a.unwrap();
let left = |o_a: Option<_>| o_a.unwrap();
//~^ ERROR overflow
let right = |o_b: Option<_>| o_b.unwrap();
iso(left, right)
//~^ ERROR overflow
}

fn main() {}
12 changes: 4 additions & 8 deletions tests/ui/traits/mutual-recursion-issue-75860.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
error[E0275]: overflow evaluating the requirement `Option<_>: Sized`
--> $DIR/mutual-recursion-issue-75860.rs:11:5
error[E0275]: overflow assigning `_` to `Option<_>`
--> $DIR/mutual-recursion-issue-75860.rs:9:33
|
LL | iso(left, right)
| ^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`mutual_recursion_issue_75860`)
note: required by an implicit `Sized` bound in `Option`
--> $SRC_DIR/core/src/option.rs:LL:COL
LL | let left = |o_a: Option<_>| o_a.unwrap();
| ^^^

error: aborting due to 1 previous error

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/type/type-check/unknown_type_for_closure.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0282]: type annotations needed
--> $DIR/unknown_type_for_closure.rs:2:13
--> $DIR/unknown_type_for_closure.rs:2:14
|
LL | let x = |b: Vec<_>| {};
| ^^^^^^^^^^^^^^ cannot infer type for struct `Vec<_>`
| ^^^^^^^^^ cannot infer type

error[E0282]: type annotations needed
--> $DIR/unknown_type_for_closure.rs:6:14
Expand Down

0 comments on commit 18fb860

Please sign in to comment.