Skip to content

Commit def7ed0

Browse files
Implement ~const Fn trait goals in the new solver
1 parent 89b6885 commit def7ed0

File tree

11 files changed

+193
-109
lines changed

11 files changed

+193
-109
lines changed

compiler/rustc_middle/src/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,11 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
374374
self.explicit_implied_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
375375
}
376376

377-
fn is_const_impl(self, def_id: DefId) -> bool {
377+
fn impl_is_const(self, def_id: DefId) -> bool {
378+
self.is_conditionally_const(def_id)
379+
}
380+
381+
fn fn_is_const(self, def_id: DefId) -> bool {
378382
self.is_conditionally_const(def_id)
379383
}
380384

compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs

+70
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,76 @@ fn coroutine_closure_to_ambiguous_coroutine<I: Interner>(
633633
)
634634
}
635635

636+
/// This duplicates `extract_tupled_inputs_and_output_from_callable` but needs
637+
/// to return different information (namely, the def id and args) so that we can
638+
/// create const conditions.
639+
///
640+
/// Doing so on all calls to `extract_tupled_inputs_and_output_from_callable`
641+
/// would be wasteful.
642+
pub(in crate::solve) fn extract_fn_def_from_const_callable<I: Interner>(
643+
cx: I,
644+
self_ty: I::Ty,
645+
) -> Result<(ty::Binder<I, (I::FnInputTys, I::Ty)>, I::DefId, I::GenericArgs), NoSolution> {
646+
match self_ty.kind() {
647+
ty::FnDef(def_id, args) => {
648+
let sig = cx.fn_sig(def_id);
649+
if sig.skip_binder().is_fn_trait_compatible()
650+
&& !cx.has_target_features(def_id)
651+
&& cx.fn_is_const(def_id)
652+
{
653+
Ok((
654+
sig.instantiate(cx, args).map_bound(|sig| (sig.inputs(), sig.output())),
655+
def_id,
656+
args,
657+
))
658+
} else {
659+
return Err(NoSolution);
660+
}
661+
}
662+
// `FnPtr`s are not const for now.
663+
ty::FnPtr(..) => {
664+
return Err(NoSolution);
665+
}
666+
// `Closure`s are not const for now.
667+
ty::Closure(..) => {
668+
return Err(NoSolution);
669+
}
670+
// `CoroutineClosure`s are not const for now.
671+
ty::CoroutineClosure(..) => {
672+
return Err(NoSolution);
673+
}
674+
675+
ty::Bool
676+
| ty::Char
677+
| ty::Int(_)
678+
| ty::Uint(_)
679+
| ty::Float(_)
680+
| ty::Adt(_, _)
681+
| ty::Foreign(_)
682+
| ty::Str
683+
| ty::Array(_, _)
684+
| ty::Slice(_)
685+
| ty::RawPtr(_, _)
686+
| ty::Ref(_, _, _)
687+
| ty::Dynamic(_, _, _)
688+
| ty::Coroutine(_, _)
689+
| ty::CoroutineWitness(..)
690+
| ty::Never
691+
| ty::Tuple(_)
692+
| ty::Pat(_, _)
693+
| ty::Alias(_, _)
694+
| ty::Param(_)
695+
| ty::Placeholder(..)
696+
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
697+
| ty::Error(_) => return Err(NoSolution),
698+
699+
ty::Bound(..)
700+
| ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
701+
panic!("unexpected type `{self_ty:?}`")
702+
}
703+
}
704+
}
705+
636706
/// Assemble a list of predicates that would be present on a theoretical
637707
/// user impl for an object type. These predicates must be checked any time
638708
/// we assemble a built-in object candidate for an object type, since they

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

+46-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
44
use rustc_type_ir::fast_reject::DeepRejectCtxt;
55
use rustc_type_ir::inherent::*;
6+
use rustc_type_ir::lang_items::TraitSolverLangItem;
67
use rustc_type_ir::{self as ty, Interner, elaborate};
78
use tracing::instrument;
89

9-
use super::assembly::Candidate;
10+
use super::assembly::{Candidate, structural_traits};
1011
use crate::delegate::SolverDelegate;
11-
use crate::solve::assembly::{self};
1212
use crate::solve::{
1313
BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, NoSolution,
14-
QueryResult,
14+
QueryResult, assembly,
1515
};
1616

1717
impl<D, I> assembly::GoalKind<D> for ty::HostEffectPredicate<I>
@@ -142,7 +142,7 @@ where
142142
ty::ImplPolarity::Positive => {}
143143
};
144144

145-
if !cx.is_const_impl(impl_def_id) {
145+
if !cx.impl_is_const(impl_def_id) {
146146
return Err(NoSolution);
147147
}
148148

@@ -207,7 +207,7 @@ where
207207
_ecx: &mut EvalCtxt<'_, D>,
208208
_goal: Goal<I, Self>,
209209
) -> Result<Candidate<I>, NoSolution> {
210-
todo!("Copy/Clone is not yet const")
210+
Err(NoSolution)
211211
}
212212

213213
fn consider_builtin_pointer_like_candidate(
@@ -225,11 +225,48 @@ where
225225
}
226226

227227
fn consider_builtin_fn_trait_candidates(
228-
_ecx: &mut EvalCtxt<'_, D>,
229-
_goal: Goal<I, Self>,
228+
ecx: &mut EvalCtxt<'_, D>,
229+
goal: Goal<I, Self>,
230230
_kind: rustc_type_ir::ClosureKind,
231231
) -> Result<Candidate<I>, NoSolution> {
232-
todo!("Fn* are not yet const")
232+
let cx = ecx.cx();
233+
234+
let self_ty = goal.predicate.self_ty();
235+
let (inputs_and_output, def_id, args) =
236+
structural_traits::extract_fn_def_from_const_callable(cx, self_ty)?;
237+
238+
// A built-in `Fn` impl only holds if the output is sized.
239+
// (FIXME: technically we only need to check this if the type is a fn ptr...)
240+
let output_is_sized_pred = inputs_and_output.map_bound(|(_, output)| {
241+
ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output])
242+
});
243+
let requirements = cx
244+
.const_conditions(def_id)
245+
.iter_instantiated(cx, args)
246+
.map(|trait_ref| {
247+
(
248+
GoalSource::ImplWhereBound,
249+
goal.with(cx, trait_ref.to_host_effect_clause(cx, goal.predicate.constness)),
250+
)
251+
})
252+
.chain([(GoalSource::ImplWhereBound, goal.with(cx, output_is_sized_pred))]);
253+
254+
let pred = inputs_and_output
255+
.map_bound(|(inputs, _)| {
256+
ty::TraitRef::new(cx, goal.predicate.def_id(), [
257+
goal.predicate.self_ty(),
258+
Ty::new_tup(cx, inputs.as_slice()),
259+
])
260+
})
261+
.to_host_effect_clause(cx, goal.predicate.constness);
262+
263+
Self::probe_and_consider_implied_clause(
264+
ecx,
265+
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
266+
goal,
267+
pred,
268+
requirements,
269+
)
233270
}
234271

235272
fn consider_builtin_async_fn_trait_candidates(
@@ -314,7 +351,7 @@ where
314351
_ecx: &mut EvalCtxt<'_, D>,
315352
_goal: Goal<I, Self>,
316353
) -> Result<Candidate<I>, NoSolution> {
317-
unreachable!("Destruct is not const")
354+
Err(NoSolution)
318355
}
319356

320357
fn consider_builtin_transmute_candidate(

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ where
394394
return ecx.forced_ambiguity(MaybeCause::Ambiguity);
395395
}
396396
};
397+
398+
// A built-in `Fn` impl only holds if the output is sized.
399+
// (FIXME: technically we only need to check this if the type is a fn ptr...)
397400
let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| {
398401
ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output])
399402
});
@@ -408,8 +411,6 @@ where
408411
})
409412
.upcast(cx);
410413

411-
// A built-in `Fn` impl only holds if the output is sized.
412-
// (FIXME: technically we only need to check this if the type is a fn ptr...)
413414
Self::probe_and_consider_implied_clause(
414415
ecx,
415416
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
@@ -438,6 +439,9 @@ where
438439
goal_kind,
439440
env_region,
440441
)?;
442+
443+
// A built-in `AsyncFn` impl only holds if the output is sized.
444+
// (FIXME: technically we only need to check this if the type is a fn ptr...)
441445
let output_is_sized_pred = tupled_inputs_and_output_and_coroutine.map_bound(
442446
|AsyncCallableRelevantTypes { output_coroutine_ty: output_ty, .. }| {
443447
ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output_ty])
@@ -494,8 +498,6 @@ where
494498
)
495499
.upcast(cx);
496500

497-
// A built-in `AsyncFn` impl only holds if the output is sized.
498-
// (FIXME: technically we only need to check this if the type is a fn ptr...)
499501
Self::probe_and_consider_implied_clause(
500502
ecx,
501503
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ where
326326
return ecx.forced_ambiguity(MaybeCause::Ambiguity);
327327
}
328328
};
329+
330+
// A built-in `Fn` impl only holds if the output is sized.
331+
// (FIXME: technically we only need to check this if the type is a fn ptr...)
329332
let output_is_sized_pred = tupled_inputs_and_output.map_bound(|(_, output)| {
330333
ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [output])
331334
});
@@ -335,8 +338,6 @@ where
335338
ty::TraitRef::new(cx, goal.predicate.def_id(), [goal.predicate.self_ty(), inputs])
336339
})
337340
.upcast(cx);
338-
// A built-in `Fn` impl only holds if the output is sized.
339-
// (FIXME: technically we only need to check this if the type is a fn ptr...)
340341
Self::probe_and_consider_implied_clause(
341342
ecx,
342343
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
@@ -364,6 +365,9 @@ where
364365
// This region doesn't matter because we're throwing away the coroutine type
365366
Region::new_static(cx),
366367
)?;
368+
369+
// A built-in `AsyncFn` impl only holds if the output is sized.
370+
// (FIXME: technically we only need to check this if the type is a fn ptr...)
367371
let output_is_sized_pred = tupled_inputs_and_output_and_coroutine.map_bound(
368372
|AsyncCallableRelevantTypes { output_coroutine_ty, .. }| {
369373
ty::TraitRef::new(cx, cx.require_lang_item(TraitSolverLangItem::Sized), [
@@ -380,8 +384,6 @@ where
380384
])
381385
})
382386
.upcast(cx);
383-
// A built-in `AsyncFn` impl only holds if the output is sized.
384-
// (FIXME: technically we only need to check this if the type is a fn ptr...)
385387
Self::probe_and_consider_implied_clause(
386388
ecx,
387389
CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),

compiler/rustc_type_ir/src/interner.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ pub trait Interner:
223223
def_id: Self::DefId,
224224
) -> ty::EarlyBinder<Self, impl IntoIterator<Item = (Self::Clause, Self::Span)>>;
225225

226-
fn is_const_impl(self, def_id: Self::DefId) -> bool;
226+
fn impl_is_const(self, def_id: Self::DefId) -> bool;
227+
fn fn_is_const(self, def_id: Self::DefId) -> bool;
227228
fn const_conditions(
228229
self,
229230
def_id: Self::DefId,

tests/ui/traits/const-traits/const-fns-are-early-bound.rs

-90
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ aux-build:minicore.rs
2+
//@ compile-flags: --crate-type=lib -Znext-solver -Cpanic=abort
3+
//@ check-pass
4+
5+
#![feature(no_core, const_trait_impl)]
6+
#![no_std]
7+
#![no_core]
8+
9+
extern crate minicore;
10+
use minicore::*;
11+
12+
fn is_const_fn<F>(_: F)
13+
where
14+
F: const FnOnce(),
15+
{
16+
}
17+
18+
const fn foo() {}
19+
20+
fn test() {
21+
is_const_fn(foo);
22+
}

0 commit comments

Comments
 (0)