Skip to content

Fix ICE in ParamConst::find_ty_from_env when handling None values #139333

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

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
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
param_env: ty::ParamEnv<'tcx>,
placeholder: Self::PlaceholderConst,
) -> Ty<'tcx> {
placeholder.find_const_ty_from_env(param_env)
placeholder.find_const_ty_from_env_unwrap(param_env)
}

fn anonymize_bound_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
Expand Down
14 changes: 11 additions & 3 deletions compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ pub struct Placeholder<T> {
pub bound: T,
}
impl Placeholder<BoundVar> {
pub fn find_const_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
pub fn find_const_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Option<Ty<'tcx>> {
let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
// `ConstArgHasType` are never desugared to be higher ranked.
match clause.kind().skip_binder() {
Expand All @@ -864,10 +864,18 @@ impl Placeholder<BoundVar> {
}
});

let ty = candidates.next().unwrap();
assert!(candidates.next().is_none());
let ty = candidates.next();
if ty.is_some() {
assert!(candidates.next().is_none());
}
ty
}

/// Same as `find_const_ty_from_env` but unwraps the result.
/// This will panic if no type is found for the const parameter.
pub fn find_const_ty_from_env_unwrap<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
self.find_const_ty_from_env(env).unwrap()
}
Comment on lines +874 to +878
Copy link
Member

Choose a reason for hiding this comment

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

This is the opposite of our usual conventions: we do not qualify the unwrapping function in its name, instead we qualify the Option-returning variant of a function as try, for a try_something.

}

pub type PlaceholderRegion = Placeholder<BoundRegion>;
Expand Down
15 changes: 12 additions & 3 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl ParamConst {
}

#[instrument(level = "debug")]
pub fn find_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
pub fn find_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Option<Ty<'tcx>> {
let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
// `ConstArgHasType` are never desugared to be higher ranked.
match clause.kind().skip_binder() {
Expand All @@ -358,10 +358,19 @@ impl ParamConst {
}
});

let ty = candidates.next().unwrap();
assert!(candidates.next().is_none());
let ty = candidates.next();
if ty.is_some() {
assert!(candidates.next().is_none());
}
ty
}

/// Same as `find_ty_from_env` but unwraps the result.
/// This will panic if no type is found for the const parameter.
#[instrument(level = "debug")]
pub fn find_ty_from_env_unwrap<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
self.find_ty_from_env(env).unwrap()
}
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ pub(super) fn fulfillment_error_for_no_solution<'tcx>(
ty::ConstKind::Unevaluated(uv) => {
infcx.tcx.type_of(uv.def).instantiate(infcx.tcx, uv.args)
}
ty::ConstKind::Param(param_ct) => param_ct.find_ty_from_env(obligation.param_env),
ty::ConstKind::Param(param_ct) => {
match param_ct.find_ty_from_env(obligation.param_env) {
Some(ty) => ty,
None => {
// If we can't find the type, use error type
Ty::new_misc_error(infcx.tcx)
}
}
},
ty::ConstKind::Value(cv) => cv.ty,
kind => span_bug!(
obligation.cause.span,
Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,15 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
}
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
ty::ConstKind::Param(param_ct) => {
param_ct.find_ty_from_env(obligation.param_env)
match param_ct.find_ty_from_env(obligation.param_env) {
Some(ty) => ty,
None => {
// If we can't find the type, return an error
return ProcessResult::Error(FulfillmentErrorCode::Select(
SelectionError::Unimplemented
));
}
}
}
};

Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
ty::ConstKind::Bound(_, _) => bug!("escaping bound vars in {:?}", ct),
ty::ConstKind::Param(param_ct) => {
param_ct.find_ty_from_env(obligation.param_env)
match param_ct.find_ty_from_env(obligation.param_env) {
Some(ty) => ty,
None => {
// If we can't find the type, return an error
return Ok(EvaluatedToErr);
}
}
}
};

Expand Down
15 changes: 15 additions & 0 deletions tests/ui/async-await/issue-139314-option-unwrap-none-regression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test for issue #139314
// This test ensures that the compiler properly reports an error
// instead of panicking with "called `Option::unwrap()` on a `None` value"
// when processing async functions with const parameters.

//@ edition:2018
//@ error-pattern: the trait bound

async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
func(iter.map(|x| x + 1))
}

fn main() {
// Just make sure the function compiles, we don't need to call it
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-regression.rs:10:10
|
LL | func(iter.map(|x| x + 1))
| ---- ^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`
help: consider removing this method call, as the receiver has type `T` and `T: Copy` trivially holds
|
LL - func(iter.map(|x| x + 1))
LL + func(iter)
|

error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-regression.rs:10:5
|
LL | func(iter.map(|x| x + 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`

error[E0277]: the trait bound `impl Future<Output = impl Clone>: Clone` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:94
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ______________________________________________________________________________________________^
LL | | func(iter.map(|x| x + 1))
LL | | }
| |_^ the trait `Clone` is not implemented for `impl Future<Output = impl Clone>`

error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:94
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ______________________________________________________________________________________________^
LL | | func(iter.map(|x| x + 1))
LL | | }
| |_^ unsatisfied trait bound
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`

error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:1
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-regression.rs:10:19: 10:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`

error[E0277]: the trait bound `impl Future<Output = impl Clone>: Clone` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-regression.rs:9:74
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `impl Future<Output = impl Clone>`

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0277`.
16 changes: 16 additions & 0 deletions tests/ui/async-await/issue-139314-option-unwrap-none-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test for issue #139314
// This test ensures that the compiler doesn't panic with
// "called `Option::unwrap()` on a `None` value" when processing
// async functions with const parameters.

//@ edition:2018
//@ error-pattern: the trait bound

// This is a simplified version of the test case that caused the ICE
async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
func(iter.map(|x| x + 1))
}

fn main() {
// Just make sure the function compiles, we don't need to call it
}
80 changes: 80 additions & 0 deletions tests/ui/async-await/issue-139314-option-unwrap-none-test.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-test.rs:11:10
|
LL | func(iter.map(|x| x + 1))
| ---- ^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`
help: consider removing this method call, as the receiver has type `T` and `T: Copy` trivially holds
|
LL - func(iter.map(|x| x + 1))
LL + func(iter)
|

error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-test.rs:11:5
|
LL | func(iter.map(|x| x + 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`

error[E0277]: the trait bound `impl Future<Output = impl Clone>: Clone` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:94
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ______________________________________________________________________________________________^
LL | | func(iter.map(|x| x + 1))
LL | | }
| |_^ the trait `Clone` is not implemented for `impl Future<Output = impl Clone>`

error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:94
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ______________________________________________________________________________________________^
LL | | func(iter.map(|x| x + 1))
LL | | }
| |_^ unsatisfied trait bound
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`

error[E0277]: the trait bound `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>: Copy` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:1
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unsatisfied trait bound
|
= help: the trait `Copy` is not implemented for `Map<T, {closure@$DIR/issue-139314-option-unwrap-none-test.rs:11:19: 11:22}>`
note: required by a bound in `func`
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:40
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^ required by this bound in `func`

error[E0277]: the trait bound `impl Future<Output = impl Clone>: Clone` is not satisfied
--> $DIR/issue-139314-option-unwrap-none-test.rs:10:74
|
LL | async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
| ^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `impl Future<Output = impl Clone>`

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0277`.
15 changes: 15 additions & 0 deletions tests/ui/async-await/issue-139314-option-unwrap-none.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test for issue #139314
// This test ensures that the compiler doesn't panic with
// "called `Option::unwrap()` on a `None` value" when processing
// async functions with const parameters.

//@ edition:2018
//@ error-pattern: the trait bound

async fn func<T: Iterator<Item = u8> + Copy, const N: usize>(iter: T) -> impl for<'a1> Clone {
func(iter.map(|x| x + 1))
}

fn main() {
// Just make sure the function compiles, we don't need to call it
}
Loading
Loading