Skip to content

Commit faea6ad

Browse files
Check const_eval_select intrinsic correctly
1 parent 69f360d commit faea6ad

File tree

4 files changed

+81
-46
lines changed

4 files changed

+81
-46
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+59
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,65 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
471471
}
472472
}
473473

474+
if let Some(def_id) = def_id
475+
&& self.tcx.def_kind(def_id) == hir::def::DefKind::Fn
476+
&& self.tcx.is_intrinsic(def_id)
477+
&& self.tcx.item_name(def_id) == sym::const_eval_select
478+
{
479+
let fn_sig = self.resolve_vars_if_possible(fn_sig);
480+
for idx in 0..=1 {
481+
let arg_ty = fn_sig.inputs()[idx + 1];
482+
let span = arg_exprs.get(idx + 1).map_or(call_expr.span, |arg| arg.span);
483+
// Check that second and third argument of `const_eval_select` must be `FnDef`, and additionally that
484+
// the second argument must be `const fn`. The first argument must be a tuple, but this is already expressed
485+
// in the function signature (`F: FnOnce<ARG>`), so I did not bother to add another check here.
486+
//
487+
// This check is here because there is currently no way to express a trait bound for `FnDef` types only.
488+
if let ty::FnDef(def_id, _args) = *arg_ty.kind() {
489+
let fn_once_def_id =
490+
self.tcx.require_lang_item(hir::LangItem::FnOnce, Some(span));
491+
let fn_once_output_def_id =
492+
self.tcx.require_lang_item(hir::LangItem::FnOnceOutput, Some(span));
493+
if self.tcx.generics_of(fn_once_def_id).host_effect_index.is_none() {
494+
if idx == 0 && !self.tcx.is_const_fn_raw(def_id) {
495+
self.tcx.sess.emit_err(errors::ConstSelectMustBeConst { span });
496+
}
497+
} else {
498+
let const_param: ty::GenericArg<'tcx> =
499+
([self.tcx.consts.false_, self.tcx.consts.true_])[idx].into();
500+
self.register_predicate(traits::Obligation::new(
501+
self.tcx,
502+
self.misc(span),
503+
self.param_env,
504+
ty::TraitRef::new(
505+
self.tcx,
506+
fn_once_def_id,
507+
[arg_ty.into(), fn_sig.inputs()[0].into(), const_param],
508+
),
509+
));
510+
511+
self.register_predicate(traits::Obligation::new(
512+
self.tcx,
513+
self.misc(span),
514+
self.param_env,
515+
ty::ProjectionPredicate {
516+
projection_ty: ty::AliasTy::new(
517+
self.tcx,
518+
fn_once_output_def_id,
519+
[arg_ty.into(), fn_sig.inputs()[0].into(), const_param],
520+
),
521+
term: fn_sig.output().into(),
522+
},
523+
));
524+
525+
self.select_obligations_where_possible(|_| {});
526+
}
527+
} else {
528+
self.tcx.sess.emit_err(errors::ConstSelectMustBeFn { span, ty: arg_ty });
529+
}
530+
}
531+
}
532+
474533
fn_sig.output()
475534
}
476535

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

-29
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
230230
let minimum_input_count = expected_input_tys.len();
231231
let provided_arg_count = provided_args.len();
232232

233-
let is_const_eval_select = matches!(fn_def_id, Some(def_id) if
234-
self.tcx.def_kind(def_id) == hir::def::DefKind::Fn
235-
&& self.tcx.is_intrinsic(def_id)
236-
&& self.tcx.item_name(def_id) == sym::const_eval_select);
237-
238233
// We introduce a helper function to demand that a given argument satisfy a given input
239234
// This is more complicated than just checking type equality, as arguments could be coerced
240235
// This version writes those types back so further type checking uses the narrowed types
@@ -269,30 +264,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
269264
return Compatibility::Incompatible(coerce_error);
270265
}
271266

272-
// Check that second and third argument of `const_eval_select` must be `FnDef`, and additionally that
273-
// the second argument must be `const fn`. The first argument must be a tuple, but this is already expressed
274-
// in the function signature (`F: FnOnce<ARG>`), so I did not bother to add another check here.
275-
//
276-
// This check is here because there is currently no way to express a trait bound for `FnDef` types only.
277-
if is_const_eval_select && (1..=2).contains(&idx) {
278-
if let ty::FnDef(def_id, args) = *checked_ty.kind() {
279-
if idx == 1 {
280-
if !self.tcx.is_const_fn_raw(def_id) {
281-
self.tcx.sess.emit_err(errors::ConstSelectMustBeConst {
282-
span: provided_arg.span,
283-
});
284-
} else {
285-
self.enforce_context_effects(provided_arg.span, def_id, args)
286-
}
287-
}
288-
} else {
289-
self.tcx.sess.emit_err(errors::ConstSelectMustBeFn {
290-
span: provided_arg.span,
291-
ty: checked_ty,
292-
});
293-
}
294-
}
295-
296267
// 3. Check if the formal type is a supertype of the checked one
297268
// and register any such obligations for future type checks
298269
let supertype_error = self.at(&self.misc(provided_arg.span), self.param_env).sup(

tests/ui/intrinsics/const-eval-select-bad.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ LL | const_eval_select((), || {}, || {});
1616
= note: expected a function item, found {closure@$DIR/const-eval-select-bad.rs:7:34: 7:36}
1717
= help: consult the documentation on `const_eval_select` for more information
1818

19-
error: this argument must be a function item
19+
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
2020
--> $DIR/const-eval-select-bad.rs:10:27
2121
|
2222
LL | const_eval_select((), 42, 0xDEADBEEF);
23-
| ^^
23+
| ----------------- ^^ expected an `FnOnce()` closure, found `{integer}`
24+
| |
25+
| required by a bound introduced by this call
2426
|
25-
= note: expected a function item, found {integer}
26-
= help: consult the documentation on `const_eval_select` for more information
27+
= help: the trait `FnOnce<()>` is not implemented for `{integer}`
28+
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }`
29+
note: required by a bound in `const_eval_select`
30+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
2731

2832
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
29-
--> $DIR/const-eval-select-bad.rs:10:27
33+
--> $DIR/const-eval-select-bad.rs:10:31
3034
|
3135
LL | const_eval_select((), 42, 0xDEADBEEF);
32-
| ----------------- ^^ expected an `FnOnce()` closure, found `{integer}`
36+
| ----------------- ^^^^^^^^^^ expected an `FnOnce()` closure, found `{integer}`
3337
| |
3438
| required by a bound introduced by this call
3539
|
@@ -39,26 +43,22 @@ note: required by a bound in `const_eval_select`
3943
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
4044

4145
error: this argument must be a function item
42-
--> $DIR/const-eval-select-bad.rs:10:31
46+
--> $DIR/const-eval-select-bad.rs:10:27
4347
|
4448
LL | const_eval_select((), 42, 0xDEADBEEF);
45-
| ^^^^^^^^^^
49+
| ^^
4650
|
4751
= note: expected a function item, found {integer}
4852
= help: consult the documentation on `const_eval_select` for more information
4953

50-
error[E0277]: expected a `FnOnce()` closure, found `{integer}`
54+
error: this argument must be a function item
5155
--> $DIR/const-eval-select-bad.rs:10:31
5256
|
5357
LL | const_eval_select((), 42, 0xDEADBEEF);
54-
| ----------------- ^^^^^^^^^^ expected an `FnOnce()` closure, found `{integer}`
55-
| |
56-
| required by a bound introduced by this call
58+
| ^^^^^^^^^^
5759
|
58-
= help: the trait `FnOnce<()>` is not implemented for `{integer}`
59-
= note: wrap the `{integer}` in a closure with no arguments: `|| { /* code */ }`
60-
note: required by a bound in `const_eval_select`
61-
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
60+
= note: expected a function item, found {integer}
61+
= help: consult the documentation on `const_eval_select` for more information
6262

6363
error[E0271]: expected `bar` to be a fn item that returns `i32`, but it returns `bool`
6464
--> $DIR/const-eval-select-bad.rs:32:34

tests/ui/rfcs/rfc-2632-const-trait-impl/effects/minicore.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -518,5 +518,10 @@ extern "rust-intrinsic" {
518518
arg: ARG,
519519
called_in_const: F,
520520
called_at_rt: G,
521-
) -> RET;
521+
) -> RET
522+
/* where clauses enforced by built-in method confirmation:
523+
where
524+
F: const FnOnce<Arg, Output = RET>,
525+
G: FnOnce<Arg, Output = RET>,
526+
*/;
522527
}

0 commit comments

Comments
 (0)