Skip to content

New const traits syntax #139858

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl ParenthesizedArgs {

pub use crate::node_id::{CRATE_NODE_ID, DUMMY_NODE_ID, NodeId};

/// Modifiers on a trait bound like `~const`, `?` and `!`.
/// Modifiers on a trait bound like `[const]`, `?` and `!`.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug)]
pub struct TraitBoundModifiers {
pub constness: BoundConstness,
Expand Down Expand Up @@ -3111,7 +3111,7 @@ pub enum BoundConstness {
Never,
/// `Type: const Trait`
Always(Span),
/// `Type: ~const Trait`
/// `Type: [const] Trait`
Maybe(Span),
}

Expand All @@ -3120,7 +3120,7 @@ impl BoundConstness {
match self {
Self::Never => "",
Self::Always(_) => "const",
Self::Maybe(_) => "~const",
Self::Maybe(_) => "[const]",
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,17 @@ ast_passes_static_without_body =
free static item without body
.suggestion = provide a definition for the static

ast_passes_tilde_const_disallowed = `~const` is not allowed here
.closure = closures cannot have `~const` trait bounds
.function = this function is not `const`, so it cannot have `~const` trait bounds
.trait = this trait is not a `#[const_trait]`, so it cannot have `~const` trait bounds
.trait_impl = this impl is not `const`, so it cannot have `~const` trait bounds
.impl = inherent impls cannot have `~const` trait bounds
.trait_assoc_ty = associated types in non-`#[const_trait]` traits cannot have `~const` trait bounds
.trait_impl_assoc_ty = associated types in non-const impls cannot have `~const` trait bounds
.inherent_assoc_ty = inherent associated types cannot have `~const` trait bounds
.object = trait objects cannot have `~const` trait bounds
.item = this item cannot have `~const` trait bounds
ast_passes_tilde_const_disallowed = `[const]` is not allowed here
.closure = closures cannot have `[const]` trait bounds
.function = this function is not `const`, so it cannot have `[const]` trait bounds
.trait = this trait is not a `#[const_trait]`, so it cannot have `[const]` trait bounds
.trait_impl = this impl is not `const`, so it cannot have `[const]` trait bounds
.impl = inherent impls cannot have `[const]` trait bounds
.trait_assoc_ty = associated types in non-`#[const_trait]` traits cannot have `[const]` trait bounds
.trait_impl_assoc_ty = associated types in non-const impls cannot have `[const]` trait bounds
.inherent_assoc_ty = inherent associated types cannot have `[const]` trait bounds
.object = trait objects cannot have `[const]` trait bounds
.item = this item cannot have `[const]` trait bounds

ast_passes_trait_fn_const =
functions in {$in_impl ->
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/check_consts/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
Some(ConstConditionsHold::Yes)
} else {
tcx.dcx()
.span_delayed_bug(call_span, "this should have reported a ~const error in HIR");
.span_delayed_bug(call_span, "this should have reported a [const] error in HIR");
Some(ConstConditionsHold::No)
}
}
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 @@ -149,7 +149,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
debug!(?param_ty);
if let Some(generics) = tcx.hir_node_by_def_id(caller).generics() {
let constraint = with_no_trimmed_paths!(format!(
"~const {}",
"[const] {}",
trait_ref.print_trait_sugared(),
));
suggest_constraining_type_param(
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_const_eval/src/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ impl Qualif for NeedsNonConstDrop {

#[instrument(level = "trace", skip(cx), ret)]
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
// If this doesn't need drop at all, then don't select `~const Destruct`.
// If this doesn't need drop at all, then don't select `[const] Destruct`.
if !ty.needs_drop(cx.tcx, cx.typing_env) {
return false;
}

// We check that the type is `~const Destruct` since that will verify that
// the type is both `~const Drop` (if a drop impl exists for the adt), *and*
// that the components of this type are also `~const Destruct`. This
// We check that the type is `[const] Destruct` since that will verify that
// the type is both `[const] Drop` (if a drop impl exists for the adt), *and*
// that the components of this type are also `[const] Destruct`. This
// amounts to verifying that there are no values in this ADT that may have
// a non-const drop.
let destruct_def_id = cx.tcx.require_lang_item(LangItem::Destruct, cx.body.span);
Expand All @@ -203,9 +203,9 @@ impl Qualif for NeedsNonConstDrop {
fn is_structural_in_adt_value<'tcx>(cx: &ConstCx<'_, 'tcx>, adt: AdtDef<'tcx>) -> bool {
// As soon as an ADT has a destructor, then the drop becomes non-structural
// in its value since:
// 1. The destructor may have `~const` bounds which are not present on the type.
// 1. The destructor may have `[const]` bounds which are not present on the type.
// Someone needs to check that those are satisfied.
// While this could be instead satisfied by checking that the `~const Drop`
// While this could be instead satisfied by checking that the `[const] Drop`
// impl holds (i.e. replicating part of the `in_any_value_of_ty` logic above),
// even in this case, we have another problem, which is,
// 2. The destructor may *modify* the operand being dropped, so even if we
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ declare_features! (
Some("at compile-time, pointers do not have an integer value, so these casts cannot be properly supported"), 87020),
/// Allows `T: ?const Trait` syntax in bounds.
(removed, const_trait_bound_opt_out, "1.56.0", Some(67794),
Some("Removed in favor of `~const` bound in #![feature(const_trait_impl)]"), 88328),
Some("Removed in favor of `[const]` bound in #![feature(const_trait_impl)]"), 88328),
/// Allows using `crate` as visibility modifier, synonymous with `pub(crate)`.
(removed, crate_visibility_modifier, "1.63.0", Some(53120), Some("removed in favor of `pub(crate)`"), 97254),
/// Allows using custom attributes (RFC 572).
Expand Down Expand Up @@ -122,7 +122,7 @@ declare_features! (
/// [^1]: Formerly known as "object safe".
(removed, dyn_compatible_for_dispatch, "1.87.0", Some(43561),
Some("removed, not used heavily and represented additional complexity in dyn compatibility"), 136522),
/// Uses generic effect parameters for ~const bounds
/// Uses generic effect parameters for [const] bounds
(removed, effects, "1.84.0", Some(102090),
Some("removed, redundant with `#![feature(const_trait_impl)]`"), 132479),
/// Allows defining `existential type`s.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ declare_features! (
(unstable, const_async_blocks, "1.53.0", Some(85368)),
/// Allows `const || {}` closures in const contexts.
(incomplete, const_closures, "1.68.0", Some(106003)),
/// Allows using `~const Destruct` bounds and calling drop impls in const contexts.
/// Allows using `[const] Destruct` bounds and calling drop impls in const contexts.
(unstable, const_destruct, "1.85.0", Some(133214)),
/// Allows `for _ in _` loops in const contexts.
(unstable, const_for, "1.56.0", Some(87575)),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn check_opaque_meets_bounds<'tcx>(
let misc_cause = ObligationCause::misc(span, def_id);
// FIXME: We should just register the item bounds here, rather than equating.
// FIXME(const_trait_impl): When we do that, please make sure to also register
// the `~const` bounds.
// the `[const]` bounds.
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
Ok(()) => {}
Err(ty_err) => {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ fn compare_method_predicate_entailment<'tcx>(
}

// If we're within a const implementation, we need to make sure that the method
// does not assume stronger `~const` bounds than the trait definition.
// does not assume stronger `[const]` bounds than the trait definition.
//
// This registers the `~const` bounds of the impl method, which we will prove
// This registers the `[const]` bounds of the impl method, which we will prove
// using the hybrid param-env that we earlier augmented with the const conditions
// from the impl header and trait method declaration.
if is_conditionally_const {
Expand Down Expand Up @@ -2335,7 +2335,7 @@ pub(super) fn check_type_bounds<'tcx>(
)
.collect();

// Only in a const implementation do we need to check that the `~const` item bounds hold.
// Only in a const implementation do we need to check that the `[const]` item bounds hold.
if tcx.is_conditionally_const(impl_ty_def_id) {
obligations.extend(util::elaborate(
tcx,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,7 @@ fn check_impl<'tcx>(
}
}

// Ensure that the `~const` where clauses of the trait hold for the impl.
// Ensure that the `[const]` where clauses of the trait hold for the impl.
if tcx.is_conditionally_const(item.owner_id.def_id) {
for (bound, _) in
tcx.const_conditions(trait_ref.def_id).instantiate(tcx, trait_ref.args)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn associated_type_bounds<'tcx>(
);
icx.lowerer().add_default_traits(&mut bounds, item_ty, hir_bounds, None, span);
}
// `ConstIfConst` is only interested in `~const` bounds.
// `ConstIfConst` is only interested in `[const]` bounds.
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
}

Expand Down Expand Up @@ -351,7 +351,7 @@ fn opaque_type_bounds<'tcx>(
);
icx.lowerer().add_default_traits(&mut bounds, item_ty, hir_bounds, None, span);
}
//`ConstIfConst` is only interested in `~const` bounds.
//`ConstIfConst` is only interested in `[const]` bounds.
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
}
debug!(?bounds);
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
item.span,
);
}
//`ConstIfConst` is only interested in `~const` bounds.
//`ConstIfConst` is only interested in `[const]` bounds.
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
}

Expand Down Expand Up @@ -821,7 +821,7 @@ pub(super) fn assert_only_contains_predicates_from<'tcx>(
assert_eq!(
pred.constness,
ty::BoundConstness::Maybe,
"expected `~const` predicate when computing `{filter:?}` \
"expected `[const]` predicate when computing `{filter:?}` \
implied bounds: {clause:?}",
);
assert_eq!(
Expand Down Expand Up @@ -1009,7 +1009,7 @@ pub(super) fn const_conditions<'tcx>(
}
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
},
// While associated types are not really const, we do allow them to have `~const`
// While associated types are not really const, we do allow them to have `[const]`
// bounds and where clauses. `const_conditions` is responsible for gathering
// these up so we can check them in `compare_type_predicate_entailment`, and
// in `HostEffect` goal computation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
self.suggest_adding_type_and_const_args(err);
}
ExcessTypesOrConsts { .. } => {
// this can happen with `~const T` where T isn't a const_trait.
// this can happen with `[const] T` where T isn't a const_trait.
}
_ => unreachable!(),
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
}
hir::GenericBound::Outlives(lifetime) => {
// `ConstIfConst` is only interested in `~const` bounds.
// `ConstIfConst` is only interested in `[const]` bounds.
if matches!(
predicate_filter,
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst
Expand Down Expand Up @@ -708,7 +708,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
// SelfTraitThatDefines is only interested in trait predicates.
PredicateFilter::SelfTraitThatDefines(_) => {}
// `ConstIfConst` is only interested in `~const` bounds.
// `ConstIfConst` is only interested in `[const]` bounds.
PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ pub enum PredicateFilter {
/// and `<Self as Tr>::A: B`.
SelfAndAssociatedTypeBounds,

/// Filter only the `~const` bounds, which are lowered into `HostEffect` clauses.
/// Filter only the `[const]` bounds, which are lowered into `HostEffect` clauses.
ConstIfConst,

/// Filter only the `~const` bounds which are *also* in the supertrait position.
/// Filter only the `[const]` bounds which are *also* in the supertrait position.
SelfConstIfConst,
}

Expand Down Expand Up @@ -885,7 +885,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
}
// On the flip side, when filtering `ConstIfConst` bounds, we only need to convert
// `~const` bounds. All other predicates are handled in their respective queries.
// `[const]` bounds. All other predicates are handled in their respective queries.
//
// Note that like `PredicateFilter::SelfOnly`, we don't need to do any filtering
// here because we only call this on self bounds, and deal with the recursive case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,22 +402,22 @@ fn check_predicates<'tcx>(
/// as some predicate on the base impl (`predicate2`).
///
/// This basically just checks syntactic equivalence, but is a little more
/// forgiving since we want to equate `T: Tr` with `T: ~const Tr` so this can work:
/// forgiving since we want to equate `T: Tr` with `T: [const] Tr` so this can work:
///
/// ```ignore (illustrative)
/// #[rustc_specialization_trait]
/// trait Specialize { }
///
/// impl<T: Bound> Tr for T { }
/// impl<T: ~const Bound + Specialize> const Tr for T { }
/// impl<T: [const] Bound + Specialize> const Tr for T { }
/// ```
///
/// However, we *don't* want to allow the reverse, i.e., when the bound on the
/// specializing impl is not as const as the bound on the base impl:
///
/// ```ignore (illustrative)
/// impl<T: ~const Bound> const Tr for T { }
/// impl<T: Bound + Specialize> const Tr for T { } // should be T: ~const Bound
/// impl<T: [const] Bound> const Tr for T { }
/// impl<T: Bound + Specialize> const Tr for T { } // should be T: [const] Bound
/// ```
///
/// So we make that check in this function and try to raise a helpful error message.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ impl<'a> State<'a> {
match constness {
hir::BoundConstness::Never => {}
hir::BoundConstness::Always(_) => self.word("const"),
hir::BoundConstness::Maybe(_) => self.word("~const"),
hir::BoundConstness::Maybe(_) => self.word("[const]"),
}
match polarity {
hir::BoundPolarity::Positive => {}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}

// If we have `rustc_do_not_const_check`, do not check `~const` bounds.
// If we have `rustc_do_not_const_check`, do not check `[const]` bounds.
if self.tcx.has_attr(self.body_id, sym::rustc_do_not_const_check) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
}
Adjust::Deref(None) => {
// FIXME(const_trait_impl): We *could* enforce `&T: ~const Deref` here.
// FIXME(const_trait_impl): We *could* enforce `&T: [const] Deref` here.
}
Adjust::Pointer(_pointer_coercion) => {
// FIXME(const_trait_impl): We should probably enforce these.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// instead of regular stability.
///
/// This enforces *syntactical* const stability of const traits. In other words,
/// it enforces the ability to name `~const`/`const` traits in trait bounds in various
/// it enforces the ability to name `[const]`/`const` traits in trait bounds in various
/// syntax positions in HIR (including in the trait of an impl header).
pub fn check_const_stability(self, def_id: DefId, span: Span, const_kw_span: Span) {
let is_staged_api = self.lookup_stability(def_id.krate.as_def_id()).is_some();
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,14 +850,14 @@ rustc_queries! {
}

/// Compute the conditions that need to hold for a conditionally-const item to be const.
/// That is, compute the set of `~const` where clauses for a given item.
/// That is, compute the set of `[const]` where clauses for a given item.
///
/// This can be thought of as the `~const` equivalent of `predicates_of`. These are the
/// This can be thought of as the `[const]` equivalent of `predicates_of`. These are the
/// predicates that need to be proven at usage sites, and can be assumed at definition.
///
/// This query also computes the `~const` where clauses for associated types, which are
/// not "const", but which have item bounds which may be `~const`. These must hold for
/// the `~const` item bound to hold.
/// This query also computes the `[const]` where clauses for associated types, which are
/// not "const", but which have item bounds which may be `[const]`. These must hold for
/// the `[const]` item bound to hold.
query const_conditions(
key: DefId
) -> ty::ConstConditions<'tcx> {
Expand All @@ -869,13 +869,13 @@ rustc_queries! {

/// Compute the const bounds that are implied for a conditionally-const item.
///
/// This can be though of as the `~const` equivalent of `explicit_item_bounds`. These
/// This can be though of as the `[const]` equivalent of `explicit_item_bounds`. These
/// are the predicates that need to proven at definition sites, and can be assumed at
/// usage sites.
query explicit_implied_const_bounds(
key: DefId
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
desc { |tcx| "computing the implied `~const` bounds for `{}`",
desc { |tcx| "computing the implied `[const]` bounds for `{}`",
tcx.def_path_str(key)
}
separate_provide_extern
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ impl<'tcx> GenericPredicates<'tcx> {
}
}

/// `~const` bounds for a given item. This is represented using a struct much like
/// `[const]` bounds for a given item. This is represented using a struct much like
/// `GenericPredicates`, where you can either choose to only instantiate the "own"
/// bounds or all of the bounds including those from the parent. This distinction
/// is necessary for code like `compare_method_predicate_entailment`.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ impl<'tcx> TyCtxt<'tcx> {
},
DefKind::Closure => {
// Closures and RPITs will eventually have const conditions
// for `~const` bounds.
// for `[const]` bounds.
false
}
DefKind::Ctor(_, CtorKind::Const)
Expand Down
Loading
Loading