Skip to content

Commit

Permalink
Auto merge of rust-lang#132479 - compiler-errors:fx-feat-yeet, r=fee1…
Browse files Browse the repository at this point in the history
…-dead

Yeet the `effects` feature, move it onto `const_trait_impl`

This PR merges the `effects` feature into the `const_trait_impl` feature. There's really no need to have two feature gates for one feature.

After this PR, if `const_trait_impl` **is** enabled:
* Users can use and define const traits
* `HostEffect` const conditions will be enforced on the HIR
* We re-check the predicates in MIR just to make sure that we don't "leak" anything during MIR lowering

And if `const_trait_impl` **is not** enabled:
* Users cannot use nor define const traits
* `HostEffect` const conditions are not enforced on the HIR
* We will raise a const validation error if we call a function that has any const conditions (i.e. const traits and functions with any `~const` in their where clasues)

This should be the last step for us to be able to enable const traits in the standard library. We still need to re-constify `Drop` and `Destruct` and stuff for const traits to be particularly *useful* for some cases, but this is a good step :D

r? fee1-dead
cc `@rust-lang/project-const-traits`
  • Loading branch information
bors committed Nov 3, 2024
2 parents e3a918e + 6b96103 commit b8c8287
Show file tree
Hide file tree
Showing 221 changed files with 370 additions and 1,025 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ pub(crate) struct ConstBoundTraitObject {
pub span: Span,
}

// FIXME(effects): Consider making the note/reason the message of the diagnostic.
// FIXME(effects): Provide structured suggestions (e.g., add `const` / `#[const_trait]` here).
// FIXME(const_trait_impl): Consider making the note/reason the message of the diagnostic.
// FIXME(const_trait_impl): Provide structured suggestions (e.g., add `const` / `#[const_trait]` here).
#[derive(Diagnostic)]
#[diag(ast_passes_tilde_const_disallowed)]
pub(crate) struct TildeConstDisallowed {
Expand Down
15 changes: 5 additions & 10 deletions compiler/rustc_const_eval/src/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use rustc_mir_dataflow::Analysis;
use rustc_mir_dataflow::impls::MaybeStorageLive;
use rustc_mir_dataflow::storage::always_storage_live_locals;
use rustc_span::{Span, Symbol, sym};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::traits::{
Obligation, ObligationCause, ObligationCauseCode, ObligationCtxt,
};
Expand Down Expand Up @@ -419,13 +418,8 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {

let errors = ocx.select_all_or_error();
if !errors.is_empty() {
// FIXME(effects): Soon this should be unconditionally delaying a bug.
if matches!(call_source, CallSource::Normal) && tcx.features().effects() {
tcx.dcx()
.span_delayed_bug(call_span, "this should have reported a ~const error in HIR");
} else {
infcx.err_ctxt().report_fulfillment_errors(errors);
}
tcx.dcx()
.span_delayed_bug(call_span, "this should have reported a ~const error in HIR");
}
}
}
Expand Down Expand Up @@ -663,8 +657,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
// typeck ensures the conditions for calling a const trait method are met,
// so we only error if the trait isn't const. We try to resolve the trait
// into the concrete method, and uses that for const stability checks.
// FIXME(effects) we might consider moving const stability checks to typeck as well.
if tcx.features().effects() && trait_is_const {
// FIXME(const_trait_impl) we might consider moving const stability checks
// to typeck as well.
if tcx.features().const_trait_impl() && trait_is_const {
// This skips the check below that ensures we only call `const fn`.
is_trait = true;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
let implsrc = selcx.select(&obligation);

if let Ok(Some(ImplSource::UserDefined(data))) = implsrc {
// FIXME(effects) revisit this
// FIXME(const_trait_impl) revisit this
if !tcx.is_const_trait_impl(data.impl_def_id) {
let span = tcx.def_span(data.impl_def_id);
err.subdiagnostic(errors::NonConstImplNote { span });
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl Qualif for NeedsNonConstDrop {
return false;
}

// FIXME(effects): Reimplement const drop checking.
// FIXME(const_trait_impl): Reimplement const drop checking.
NeedsDrop::in_any_value_of_ty(cx, ty)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
// sensitive check here. But we can at least rule out functions that are not const at
// all. That said, we have to allow calling functions inside a trait marked with
// #[const_trait]. These *are* const-checked!
// FIXME(effects): why does `is_const_fn` not classify them as const?
// FIXME(const_trait_impl): why does `is_const_fn` not classify them as const?
if (!ecx.tcx.is_const_fn(def) && !ecx.tcx.is_const_default_method(def))
|| ecx.tcx.has_attr(def, sym::rustc_do_not_const_check)
{
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ declare_features! (
Some("renamed to `doc_notable_trait`")),
/// Allows using `#[unsafe_destructor_blind_to_params]` (RFC 1238).
(removed, dropck_parametricity, "1.38.0", Some(28498), None),
/// Uses generic effect parameters for ~const bounds
(removed, effects, "CURRENT_RUSTC_VERSION", Some(102090),
Some("removed, redundant with `#![feature(const_trait_impl)]`")),
/// Allows defining `existential type`s.
(removed, existential_type, "1.38.0", Some(63063),
Some("removed in favor of `#![feature(type_alias_impl_trait)]`")),
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,6 @@ declare_features! (
(unstable, doc_masked, "1.21.0", Some(44027)),
/// Allows `dyn* Trait` objects.
(incomplete, dyn_star, "1.65.0", Some(102425)),
/// Uses generic effect parameters for ~const bounds
(incomplete, effects, "1.72.0", Some(102090)),
/// Allows exhaustive pattern matching on types that contain uninhabited types.
(unstable, exhaustive_patterns, "1.13.0", Some(51085)),
/// Allows explicit tail calls via `become` expression.
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ fn compare_method_predicate_entailment<'tcx>(
trait_m_predicates.instantiate_own(tcx, trait_to_impl_args).map(|(predicate, _)| predicate),
);

// FIXME(effects): This should be replaced with a more dedicated method.
let is_conditionally_const = tcx.is_conditionally_const(impl_def_id);
if is_conditionally_const {
// Augment the hybrid param-env with the const conditions
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
if let Err(guar) = ty.error_reported() {
return ty::Const::new_error(tcx, guar).into();
}
// FIXME(effects) see if we should special case effect params here
if !infer_args && has_default {
tcx.const_param_default(param.def_id)
.instantiate(tcx, preceding_args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ fn trait_predicates_eq<'tcx>(
predicate1: ty::Predicate<'tcx>,
predicate2: ty::Predicate<'tcx>,
) -> bool {
// FIXME(effects)
// FIXME(const_trait_impl)
predicate1 == predicate2
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
(fn_sig, Some(def_id))
}
// FIXME(effects): these arms should error because we can't enforce them
// FIXME(const_trait_impl): these arms should error because we can't enforce them
ty::FnPtr(sig_tys, hdr) => (sig_tys.with(hdr), None),
_ => {
for arg in arg_exprs {
Expand Down Expand Up @@ -843,11 +843,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
callee_did: DefId,
callee_args: GenericArgsRef<'tcx>,
) {
// FIXME(effects): We should be enforcing these effects unconditionally.
// FIXME(const_trait_impl): We should be enforcing these effects unconditionally.
// This can be done as soon as we convert the standard library back to
// using const traits, since if we were to enforce these conditions now,
// we'd fail on basically every builtin trait call (i.e. `1 + 2`).
if !self.tcx.features().effects() {
if !self.tcx.features().const_trait_impl() {
return;
}

Expand All @@ -864,11 +864,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
None => return,
};

// FIXME(effects): Should this be `is_const_fn_raw`? It depends on if we move
// FIXME(const_trait_impl): Should this be `is_const_fn_raw`? It depends on if we move
// const stability checking here too, I guess.
if self.tcx.is_conditionally_const(callee_did) {
let q = self.tcx.const_conditions(callee_did);
// FIXME(effects): Use this span with a better cause code.
// FIXME(const_trait_impl): Use this span with a better cause code.
for (cond, _) in q.instantiate(self.tcx, callee_args) {
self.register_predicate(Obligation::new(
self.tcx,
Expand All @@ -878,7 +878,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
));
}
} else {
// FIXME(effects): This should eventually be caught here.
// FIXME(const_trait_impl): This should eventually be caught here.
// For now, though, we defer some const checking to MIR.
}
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3125,7 +3125,6 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

// FIXME(effects): Please remove this. It's a footgun.
/// Whether the trait impl is marked const. This does not consider stability or feature gates.
pub fn is_const_trait_impl(self, def_id: DefId) -> bool {
self.def_kind(def_id) == DefKind::Impl { of_trait: true }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_next_trait_solver/src/solve/effect_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
let cx = ecx.cx();
let mut candidates = vec![];

// FIXME(effects): We elaborate here because the implied const bounds
// FIXME(const_trait_impl): We elaborate here because the implied const bounds
// aren't necessarily elaborated. We probably should prefix this query
// with `explicit_`...
for clause in elaborate::elaborate(
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_next_trait_solver/src/solve/trait_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,6 @@ where
return Err(NoSolution);
}

// FIXME(effects): Implement this when we get const working in the new solver

// `Destruct` is automatically implemented for every type in
// non-const environments.
ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {

// `impl const Trait for Type` items forward their const stability to their
// immediate children.
// FIXME(effects): how is this supposed to interact with `#[rustc_const_stable_indirect]`?
// FIXME(const_trait_impl): how is this supposed to interact with `#[rustc_const_stable_indirect]`?
// Currently, once that is set, we do not inherit anything from the parent any more.
if const_stab.is_none() {
debug!("annotate: const_stab not found, parent = {:?}", self.parent_const_stab);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}

ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
// FIXME(effects): We should recompute the predicate with `~const`
// FIXME(const_trait_impl): We should recompute the predicate with `~const`
// if it's `const`, and if it holds, explain that this bound only
// *conditionally* holds. If that fails, we should also do selection
// to drill this down to an impl or built-in source, so we can
Expand Down Expand Up @@ -2641,7 +2641,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
_span: Span,
) -> UnsatisfiedConst {
let unsatisfied_const = UnsatisfiedConst(false);
// FIXME(effects)
// FIXME(const_trait_impl)
unsatisfied_const
}

Expand Down Expand Up @@ -3052,7 +3052,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// Make a fresh inference variable so we can determine what the generic parameters
// of the trait are.
let var = self.next_ty_var(DUMMY_SP);
// FIXME(effects)
// FIXME(const_trait_impl)
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, [ty.skip_binder(), var]);
let obligation = Obligation::new(
self.tcx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
trait_pred.skip_binder().self_ty(),
diagnostic_name,
),
// FIXME(effects, const_trait_impl) derive_const as suggestion?
// FIXME(const_trait_impl) derive_const as suggestion?
format!("#[derive({diagnostic_name})]\n"),
Applicability::MaybeIncorrect,
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
| ty::PredicateKind::Coerce(_)
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
| ty::PredicateKind::ConstEquate(..)
// FIXME(effects): We may need to do this using the higher-ranked
// FIXME(const_trait_impl): We may need to do this using the higher-ranked
// pred instead of just instantiating it with placeholders b/c of
// higher-ranked implied bound issues in the old solver.
| ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..)) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
_obligation: &PolyTraitObligation<'tcx>,
candidates: &mut SelectionCandidateSet<'tcx>,
) {
// FIXME(effects): Destruct is not const yet, and it is implemented
// by all types today in non-const setting.
candidates.vec.push(BuiltinCandidate { has_nested: false });
}

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ fn resolve_instance_raw<'tcx>(
}
} else {
debug!(" => free item");
// FIXME(effects): we may want to erase the effect param if that is present on this item.
ty::InstanceKind::Item(def_id)
};

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_type_ir/src/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ impl<I: Interner> UpcastFrom<I, TraitRef<I>> for TraitPredicate<I> {

impl<I: Interner> fmt::Debug for TraitPredicate<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// FIXME(effects) printing?
write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl CStr {
#[inline]
#[must_use]
const fn as_non_null_ptr(&self) -> NonNull<c_char> {
// FIXME(effects) replace with `NonNull::from`
// FIXME(const_trait_impl) replace with `NonNull::from`
// SAFETY: a reference is never null
unsafe { NonNull::new_unchecked(&self.inner as *const [c_char] as *mut [c_char]) }
.as_non_null_ptr()
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/ops/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
/// [nomicon]: ../../nomicon/phantom-data.html#an-exception-the-special-case-of-the-standard-library-and-its-unstable-may_dangle
#[lang = "drop"]
#[stable(feature = "rust1", since = "1.0.0")]
// FIXME(effects) #[const_trait]
// FIXME(const_trait_impl) #[const_trait]
pub trait Drop {
/// Executes the destructor for this type.
///
Expand Down
6 changes: 3 additions & 3 deletions library/core/src/ops/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use crate::marker::Tuple;
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
// FIXME(effects) #[const_trait]
// FIXME(const_trait_impl) #[const_trait]
pub trait Fn<Args: Tuple>: FnMut<Args> {
/// Performs the call operation.
#[unstable(feature = "fn_traits", issue = "29625")]
Expand Down Expand Up @@ -159,7 +159,7 @@ pub trait Fn<Args: Tuple>: FnMut<Args> {
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
// FIXME(effects) #[const_trait]
// FIXME(const_trait_impl) #[const_trait]
pub trait FnMut<Args: Tuple>: FnOnce<Args> {
/// Performs the call operation.
#[unstable(feature = "fn_traits", issue = "29625")]
Expand Down Expand Up @@ -238,7 +238,7 @@ pub trait FnMut<Args: Tuple>: FnOnce<Args> {
)]
#[fundamental] // so that regex can rely that `&str: !FnMut`
#[must_use = "closures are lazy and do nothing unless called"]
// FIXME(effects) #[const_trait]
// FIXME(const_trait_impl) #[const_trait]
pub trait FnOnce<Args: Tuple> {
/// The returned type after the call operator is used.
#[lang = "fn_once_output"]
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/slice/sort/shared/smallsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<T: FreezeMarker> UnstableSmallSortTypeImpl for T {
}
}

/// FIXME(effects) use original ipnsort approach with choose_unstable_small_sort,
/// FIXME(const_trait_impl) use original ipnsort approach with choose_unstable_small_sort,
/// as found here <https://github.com/Voultapher/sort-research-rs/blob/438fad5d0495f65d4b72aa87f0b62fc96611dff3/ipnsort/src/smallsort.rs#L83C10-L83C36>.
pub(crate) trait UnstableSmallSortFreezeTypeImpl: Sized + FreezeMarker {
fn small_sort_threshold() -> usize;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ pub(crate) fn clean_predicate<'tcx>(
ty::ClauseKind::ConstEvaluatable(..)
| ty::ClauseKind::WellFormed(..)
| ty::ClauseKind::ConstArgHasType(..)
// FIXME(effects): We can probably use this `HostEffect` pred to render `~const`.
// FIXME(const_trait_impl): We can probably use this `HostEffect` pred to render `~const`.
| ty::ClauseKind::HostEffect(_) => None,
}
}
Expand All @@ -379,7 +379,7 @@ fn clean_poly_trait_predicate<'tcx>(
cx: &mut DocContext<'tcx>,
) -> Option<WherePredicate> {
// `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
// FIXME(effects) check constness
// FIXME(const_trait_impl) check constness
if Some(pred.skip_binder().def_id()) == cx.tcx.lang_items().destruct_trait() {
return None;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ fn is_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: &Msrv) -> bool {
}

fn is_ty_const_destruct<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
// FIXME(effects, fee1-dead) revert to const destruct once it works again
// FIXME(const_trait_impl, fee1-dead) revert to const destruct once it works again
#[expect(unused)]
fn is_ty_const_destruct_unused<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, body: &Body<'tcx>) -> bool {
// Avoid selecting for simple cases, such as builtin types.
if ty::util::is_trivially_const_drop(ty) {
return true;
}

// FIXME(effects) constness
// FIXME(const_trait_impl) constness
let obligation = Obligation::new(
tcx,
ObligationCause::dummy_with_span(body.span),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn main() {}

struct D;

/* FIXME(effects)
/* FIXME(const_trait_impl)
impl const Drop for D {
fn drop(&mut self) {
todo!();
Expand All @@ -113,7 +113,7 @@ impl const Drop for D {
*/

// Lint this, since it can be dropped in const contexts
// FIXME(effects)
// FIXME(const_trait_impl)
const fn d(this: D) {}
//~^ ERROR: this could be a `const fn`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn main() {}

struct D;

/* FIXME(effects)
/* FIXME(const_trait_impl)
impl const Drop for D {
fn drop(&mut self) {
todo!();
Expand All @@ -113,7 +113,7 @@ impl const Drop for D {
*/

// Lint this, since it can be dropped in const contexts
// FIXME(effects)
// FIXME(const_trait_impl)
fn d(this: D) {}
//~^ ERROR: this could be a `const fn`

Expand Down
Loading

0 comments on commit b8c8287

Please sign in to comment.