Skip to content
Draft
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
7 changes: 6 additions & 1 deletion compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
let constness = self.lower_constness(*constness);
let impl_restriction = self.lower_impl_restriction(impl_restriction);
let ident = self.lower_ident(*ident);
let policy = if self.tcx.features().move_trait() {
RelaxedBoundPolicy::Allowed(&mut Default::default())
} else {
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait)
};
let (generics, (safety, items, bounds)) = self.lower_generics(
generics,
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
|this| {
let bounds = this.lower_param_bounds(
bounds,
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait),
policy,
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
);
let items = this.arena.alloc_from_iter(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
ConstraintCategory::TypeAnnotation(_) => "type annotation ",
ConstraintCategory::SizedBound => "proving this value is `Sized` ",
ConstraintCategory::MoveBound => "proving this value is `Move` ",
ConstraintCategory::CopyBound => "copying this value ",
ConstraintCategory::OpaqueType => "opaque type ",
ConstraintCategory::ClosureUpvar(_) => "closure capture ",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
| ConstraintCategory::CallArgument(_)
| ConstraintCategory::CopyBound
| ConstraintCategory::SizedBound
| ConstraintCategory::MoveBound
| ConstraintCategory::Assignment
| ConstraintCategory::Usage
| ConstraintCategory::ClosureUpvar(_) => 2,
Expand Down
28 changes: 27 additions & 1 deletion compiler/rustc_borrowck/src/type_check/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use rustc_middle::mir::{Body, ConstraintCategory};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, Unnormalized, Upcast};
use rustc_span::Span;
use rustc_span::def_id::DefId;
use rustc_trait_selection::traits::ObligationCause;
use rustc_trait_selection::traits::query::type_op::custom::FallibleCustomTypeOp;
use rustc_trait_selection::traits::query::type_op::{self, TypeOpOutput};
use rustc_trait_selection::traits::{Obligation, ObligationCause};
use tracing::{debug, instrument};

use super::{Locations, NormalizeLocation, TypeChecker};
Expand Down Expand Up @@ -183,6 +184,31 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
);
}

/// Certain proofs (e.g. `Move`) may error during MIR typeck, so handle them separately.
pub(super) fn prove_fallible_predicate(
&mut self,
predicate: impl Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>> + std::fmt::Debug,
locations: Locations,
category: ConstraintCategory<'tcx>,
) {
let span = self.last_span;
let predicate = predicate.upcast(self.tcx());
let op = FallibleCustomTypeOp::new(
|ocx| {
ocx.register_obligation(Obligation::new(
ocx.infcx.tcx,
ObligationCause::dummy_with_span(span),
self.infcx.param_env,
predicate,
));
Ok(())
},
"fallible type op",
);

let _: Result<_, ErrorGuaranteed> = self.fully_perform_op(locations, category, op);
}

pub(super) fn normalize<T>(
&mut self,
value: Unnormalized<'tcx, T>,
Expand Down
35 changes: 30 additions & 5 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_infer::infer::region_constraints::RegionConstraintData;
use rustc_infer::infer::{
BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin,
};
use rustc_infer::traits::PredicateObligations;
use rustc_infer::traits::{PredicateObligations, ScrubbedTraitError};
use rustc_middle::bug;
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
Expand Down Expand Up @@ -1871,6 +1871,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
// it must.
self.prove_trait_ref(trait_ref, location.to_locations(), ConstraintCategory::CopyBound);
}

if tcx.features().move_trait()
&& matches!(
context,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
)
{

@lcnr lcnr May 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hmm, could u do a separate if tcx.features().move_trait() { match context { exhaustivethingy } }

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

exhaustive match pls

let trait_ref = ty::TraitRef::new(
tcx,
tcx.require_lang_item(rustc_hir::LangItem::Move, self.last_span),
[place_ty.ty],
);
self.prove_fallible_predicate(
trait_ref,
location.to_locations(),
ConstraintCategory::MoveBound,
);
}
}

fn visit_projection_elem(
Expand Down Expand Up @@ -2722,10 +2741,16 @@ impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
span: Span,
) -> Result<TypeOpOutput<'tcx, Self>, ErrorGuaranteed> {
let (mut output, region_constraints) =
scrape_region_constraints(infcx, root_def_id, "InstantiateOpaqueType", span, |ocx| {
ocx.register_obligations(self.obligations.clone());
Ok(())
})?;
scrape_region_constraints::<_, _, ScrubbedTraitError<'tcx>>(
infcx,
root_def_id,
"InstantiateOpaqueType",
span,
|ocx| {
ocx.register_obligations(self.obligations.clone());
Ok(())
},
)?;
self.region_constraints = Some(region_constraints);
output.error_info = Some(self);
Ok(output)
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ declare_features! (
(unstable, more_qualified_paths, "1.54.0", Some(86935)),
/// Allows `move(expr)` in closures.
(incomplete, move_expr, "1.97.0", Some(155050)),
/// The `Move` autotrait.
(incomplete, move_trait, "CURRENT_RUSTC_VERSION", Some(149607)),
/// The `movrs` target feature on x86.
(unstable, movrs_target_feature, "1.88.0", Some(137976)),
/// Allows the `multiple_supertrait_upcastable` lint.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ language_item_table! {
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
Pin, sym::pin, pin_type, Target::Struct, GenericRequirement::None;

Move, sym::move_trait, move_trait, Target::Trait, GenericRequirement::None;

OrderingEnum, sym::Ordering, ordering_enum, Target::Enum, GenericRequirement::Exact(0);
PartialEq, sym::eq, eq_trait, Target::Trait, GenericRequirement::Exact(1);
PartialOrd, sym::partial_ord, partial_ord_trait, Target::Trait, GenericRequirement::Exact(1);
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_hir_analysis/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ fn bounds_from_generic_predicates<'tcx>(
ty::ClauseKind::Trait(trait_predicate) => {
let entry = types.entry(trait_predicate.self_ty()).or_default();
let def_id = trait_predicate.def_id();
if !tcx.is_default_trait(def_id) && !tcx.is_lang_item(def_id, LangItem::Sized) {
// nia: fixme: metasized
if !tcx.is_implicit_trait(def_id, false)
&& !tcx.is_lang_item(def_id, LangItem::Sized)
{
// Do not add that restriction to the list if it is a positive requirement.
entry.push(trait_predicate.def_id());
}
Expand Down
20 changes: 4 additions & 16 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,13 @@ fn associated_type_bounds<'tcx>(
| PredicateFilter::SelfTraitThatDefines(_)
| PredicateFilter::SelfAndAssociatedTypeBounds => {
// Implicit bounds are added to associated types unless a `?Trait` bound is found.
icx.lowerer().add_implicit_sizedness_bounds(
&mut bounds,
item_ty,
hir_bounds,
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
icx.lowerer().add_default_traits(
icx.lowerer().add_implicit_bounds(
&mut bounds,
item_ty,
hir_bounds,
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
true,
);

// Also collect `where Self::Assoc: Trait` from the parent trait's where clauses.
Expand Down Expand Up @@ -380,19 +374,13 @@ fn opaque_type_bounds<'tcx>(
| PredicateFilter::SelfOnly
| PredicateFilter::SelfTraitThatDefines(_)
| PredicateFilter::SelfAndAssociatedTypeBounds => {
icx.lowerer().add_implicit_sizedness_bounds(
&mut bounds,
item_ty,
hir_bounds,
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
);
icx.lowerer().add_default_traits(
icx.lowerer().add_implicit_bounds(
&mut bounds,
item_ty,
hir_bounds,
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
span,
true,
);
}
//`ConstIfConst` is only interested in `[const]` bounds.
Expand Down
56 changes: 23 additions & 33 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::find_attr;
use rustc_middle::ty::{
self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast,
self, GenericPredicates, ImplTraitInTraitData, PredicatePolarity, Ty, TyCtxt, TypeVisitable,
TypeVisitor, Upcast,
};
use rustc_middle::{bug, span_bug};
use rustc_span::{DUMMY_SP, Ident, Span};
Expand Down Expand Up @@ -69,6 +70,19 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic
.predicates
.iter()
.copied()
.filter(|p| {
if !tcx.features().move_trait() {
!p.0.as_trait_clause().is_some_and(|p| {
p.polarity() == PredicatePolarity::Positive
&& matches!(
tcx.as_lang_item(p.def_id()),
Some(rustc_hir::LangItem::Move)
)
})
} else {

@lcnr lcnr Jun 16, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this only filters the param_env if tcx.is_trait(def_id) is true

View changes since the review

true
}
})
.chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
);
}
Expand Down Expand Up @@ -196,19 +210,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
PredicateFilter::All,
OverlappingAsssocItemConstraints::Allowed,
);
icx.lowerer().add_implicit_sizedness_bounds(
&mut bounds,
tcx.types.self_param,
self_bounds,
ImpliedBoundsContext::TraitDef(def_id),
span,
);
icx.lowerer().add_default_traits(
icx.lowerer().add_implicit_bounds(
&mut bounds,
tcx.types.self_param,
self_bounds,
ImpliedBoundsContext::TraitDef(def_id),
span,
true,
);
predicates.extend(bounds);
}
Expand All @@ -235,19 +243,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
let mut bounds = Vec::new();
// Implicit bounds are added to type params unless a `?Trait` bound is found
icx.lowerer().add_implicit_sizedness_bounds(
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
);
icx.lowerer().add_default_traits(
icx.lowerer().add_implicit_bounds(
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
true,
);
trace!(?bounds);
predicates.extend(bounds);
Expand Down Expand Up @@ -688,19 +690,13 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
| PredicateFilter::SelfOnly
| PredicateFilter::SelfTraitThatDefines(_)
| PredicateFilter::SelfAndAssociatedTypeBounds => {
icx.lowerer().add_implicit_sizedness_bounds(
&mut bounds,
self_param_ty,
superbounds,
ImpliedBoundsContext::TraitDef(trait_def_id),
item.span,
);
icx.lowerer().add_default_traits(
icx.lowerer().add_implicit_bounds(
&mut bounds,
self_param_ty,
superbounds,
ImpliedBoundsContext::TraitDef(trait_def_id),
item.span,
true,
);
}
//`ConstIfConst` is only interested in `[const]` bounds.
Expand Down Expand Up @@ -990,19 +986,13 @@ impl<'tcx> ItemCtxt<'tcx> {
match param.kind {
hir::GenericParamKind::Type { .. } => {
let param_ty = self.lowerer().lower_ty_param(param.hir_id);
self.lowerer().add_implicit_sizedness_bounds(
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
);
self.lowerer().add_default_traits(
self.lowerer().add_implicit_bounds(
&mut bounds,
param_ty,
&[],
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
param.span,
true,
);
}
hir::GenericParamKind::Lifetime { .. }
Expand Down
Loading
Loading