Skip to content

Commit 6ae9d31

Browse files
committed
move trait
1 parent 3e75ded commit 6ae9d31

45 files changed

Lines changed: 441 additions & 138 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
549549
let constness = self.lower_constness(*constness);
550550
let impl_restriction = self.lower_impl_restriction(impl_restriction);
551551
let ident = self.lower_ident(*ident);
552+
let policy = if self.tcx.features().move_trait() {
553+
RelaxedBoundPolicy::Allowed(&mut Default::default())
554+
} else {
555+
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait)
556+
};
552557
let (generics, (safety, items, bounds)) = self.lower_generics(
553558
generics,
554559
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
555560
|this| {
556561
let bounds = this.lower_param_bounds(
557562
bounds,
558-
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::SuperTrait),
563+
policy,
559564
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
560565
);
561566
let items = this.arena.alloc_from_iter(

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
5252
ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
5353
ConstraintCategory::TypeAnnotation(_) => "type annotation ",
5454
ConstraintCategory::SizedBound => "proving this value is `Sized` ",
55+
ConstraintCategory::MoveBound => "proving this value is `Move` ",
5556
ConstraintCategory::CopyBound => "copying this value ",
5657
ConstraintCategory::OpaqueType => "opaque type ",
5758
ConstraintCategory::ClosureUpvar(_) => "closure capture ",

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17721772
| ConstraintCategory::CallArgument(_)
17731773
| ConstraintCategory::CopyBound
17741774
| ConstraintCategory::SizedBound
1775+
| ConstraintCategory::MoveBound
17751776
| ConstraintCategory::Assignment
17761777
| ConstraintCategory::Usage
17771778
| ConstraintCategory::ClosureUpvar(_) => 2,

compiler/rustc_borrowck/src/type_check/canonical.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use rustc_middle::mir::{Body, ConstraintCategory};
88
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, Unnormalized, Upcast};
99
use rustc_span::Span;
1010
use rustc_span::def_id::DefId;
11-
use rustc_trait_selection::traits::ObligationCause;
11+
use rustc_trait_selection::traits::query::type_op::custom::FallibleCustomTypeOp;
1212
use rustc_trait_selection::traits::query::type_op::{self, TypeOpOutput};
13+
use rustc_trait_selection::traits::{Obligation, ObligationCause};
1314
use tracing::{debug, instrument};
1415

1516
use super::{Locations, NormalizeLocation, TypeChecker};

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_infer::infer::region_constraints::RegionConstraintData;
1818
use rustc_infer::infer::{
1919
BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin,
2020
};
21-
use rustc_infer::traits::PredicateObligations;
21+
use rustc_infer::traits::{PredicateObligations, ScrubbedTraitError};
2222
use rustc_middle::bug;
2323
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
2424
use rustc_middle::mir::*;
@@ -1871,6 +1871,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
18711871
// it must.
18721872
self.prove_trait_ref(trait_ref, location.to_locations(), ConstraintCategory::CopyBound);
18731873
}
1874+
1875+
if tcx.features().move_trait()
1876+
&& matches!(
1877+
context,
1878+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy)
1879+
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Move)
1880+
)
1881+
{
1882+
let trait_ref = ty::TraitRef::new(
1883+
tcx,
1884+
tcx.require_lang_item(rustc_hir::LangItem::Move, self.last_span),
1885+
[place_ty.ty],
1886+
);
1887+
self.prove_fallible_predicate(
1888+
trait_ref,
1889+
location.to_locations(),
1890+
ConstraintCategory::MoveBound,
1891+
);
1892+
}
18741893
}
18751894

18761895
fn visit_projection_elem(
@@ -2722,10 +2741,16 @@ impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
27222741
span: Span,
27232742
) -> Result<TypeOpOutput<'tcx, Self>, ErrorGuaranteed> {
27242743
let (mut output, region_constraints) =
2725-
scrape_region_constraints(infcx, root_def_id, "InstantiateOpaqueType", span, |ocx| {
2726-
ocx.register_obligations(self.obligations.clone());
2727-
Ok(())
2728-
})?;
2744+
scrape_region_constraints::<_, _, ScrubbedTraitError<'tcx>>(
2745+
infcx,
2746+
root_def_id,
2747+
"InstantiateOpaqueType",
2748+
span,
2749+
|ocx| {
2750+
ocx.register_obligations(self.obligations.clone());
2751+
Ok(())
2752+
},
2753+
)?;
27292754
self.region_constraints = Some(region_constraints);
27302755
output.error_info = Some(self);
27312756
Ok(output)

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ declare_features! (
642642
(unstable, more_qualified_paths, "1.54.0", Some(86935)),
643643
/// Allows `move(expr)` in closures.
644644
(incomplete, move_expr, "1.97.0", Some(155050)),
645+
/// The `Move` autotrait.
646+
(incomplete, move_trait, "CURRENT_RUSTC_VERSION", Some(149607)),
645647
/// The `movrs` target feature on x86.
646648
(unstable, movrs_target_feature, "1.88.0", Some(137976)),
647649
/// Allows the `multiple_supertrait_upcastable` lint.

compiler/rustc_hir/src/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ language_item_table! {
262262
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
263263
Pin, sym::pin, pin_type, Target::Struct, GenericRequirement::None;
264264

265+
Move, sym::move_trait, move_trait, Target::Trait, GenericRequirement::None;
266+
265267
OrderingEnum, sym::Ordering, ordering_enum, Target::Enum, GenericRequirement::Exact(0);
266268
PartialEq, sym::eq, eq_trait, Target::Trait, GenericRequirement::Exact(1);
267269
PartialOrd, sym::partial_ord, partial_ord_trait, Target::Trait, GenericRequirement::Exact(1);

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ fn bounds_from_generic_predicates<'tcx>(
340340
ty::ClauseKind::Trait(trait_predicate) => {
341341
let entry = types.entry(trait_predicate.self_ty()).or_default();
342342
let def_id = trait_predicate.def_id();
343-
if !tcx.is_default_trait(def_id) && !tcx.is_lang_item(def_id, LangItem::Sized) {
343+
// nia: fixme: metasized
344+
if !tcx.is_implicit_trait(def_id, false)
345+
&& !tcx.is_lang_item(def_id, LangItem::Sized)
346+
{
344347
// Do not add that restriction to the list if it is a positive requirement.
345348
entry.push(trait_predicate.def_id());
346349
}

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,13 @@ fn associated_type_bounds<'tcx>(
5454
| PredicateFilter::SelfTraitThatDefines(_)
5555
| PredicateFilter::SelfAndAssociatedTypeBounds => {
5656
// Implicit bounds are added to associated types unless a `?Trait` bound is found.
57-
icx.lowerer().add_implicit_sizedness_bounds(
58-
&mut bounds,
59-
item_ty,
60-
hir_bounds,
61-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
62-
span,
63-
);
64-
icx.lowerer().add_default_traits(
57+
icx.lowerer().add_implicit_bounds(
6558
&mut bounds,
6659
item_ty,
6760
hir_bounds,
6861
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
6962
span,
63+
true,
7064
);
7165

7266
// Also collect `where Self::Assoc: Trait` from the parent trait's where clauses.
@@ -380,19 +374,13 @@ fn opaque_type_bounds<'tcx>(
380374
| PredicateFilter::SelfOnly
381375
| PredicateFilter::SelfTraitThatDefines(_)
382376
| PredicateFilter::SelfAndAssociatedTypeBounds => {
383-
icx.lowerer().add_implicit_sizedness_bounds(
384-
&mut bounds,
385-
item_ty,
386-
hir_bounds,
387-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
388-
span,
389-
);
390-
icx.lowerer().add_default_traits(
377+
icx.lowerer().add_implicit_bounds(
391378
&mut bounds,
392379
item_ty,
393380
hir_bounds,
394381
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
395382
span,
383+
true,
396384
);
397385
}
398386
//`ConstIfConst` is only interested in `[const]` bounds.

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use rustc_hir::def::DefKind;
77
use rustc_hir::def_id::{DefId, LocalDefId};
88
use rustc_hir::find_attr;
99
use rustc_middle::ty::{
10-
self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast,
10+
self, GenericPredicates, ImplTraitInTraitData, PredicatePolarity, Ty, TyCtxt, TypeVisitable,
11+
TypeVisitor, Upcast,
1112
};
1213
use rustc_middle::{bug, span_bug};
1314
use rustc_span::{DUMMY_SP, Ident, Span};
@@ -69,6 +70,19 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic
6970
.predicates
7071
.iter()
7172
.copied()
73+
.filter(|p| {
74+
if !tcx.features().move_trait() {
75+
!p.0.as_trait_clause().is_some_and(|p| {
76+
p.polarity() == PredicatePolarity::Positive
77+
&& matches!(
78+
tcx.as_lang_item(p.def_id()),
79+
Some(rustc_hir::LangItem::Move)
80+
)
81+
})
82+
} else {
83+
true
84+
}
85+
})
7286
.chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
7387
);
7488
}
@@ -196,19 +210,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
196210
PredicateFilter::All,
197211
OverlappingAsssocItemConstraints::Allowed,
198212
);
199-
icx.lowerer().add_implicit_sizedness_bounds(
200-
&mut bounds,
201-
tcx.types.self_param,
202-
self_bounds,
203-
ImpliedBoundsContext::TraitDef(def_id),
204-
span,
205-
);
206-
icx.lowerer().add_default_traits(
213+
icx.lowerer().add_implicit_bounds(
207214
&mut bounds,
208215
tcx.types.self_param,
209216
self_bounds,
210217
ImpliedBoundsContext::TraitDef(def_id),
211218
span,
219+
true,
212220
);
213221
predicates.extend(bounds);
214222
}
@@ -235,19 +243,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
235243
let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
236244
let mut bounds = Vec::new();
237245
// Implicit bounds are added to type params unless a `?Trait` bound is found
238-
icx.lowerer().add_implicit_sizedness_bounds(
239-
&mut bounds,
240-
param_ty,
241-
&[],
242-
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
243-
param.span,
244-
);
245-
icx.lowerer().add_default_traits(
246+
icx.lowerer().add_implicit_bounds(
246247
&mut bounds,
247248
param_ty,
248249
&[],
249250
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
250251
param.span,
252+
true,
251253
);
252254
trace!(?bounds);
253255
predicates.extend(bounds);
@@ -688,19 +690,13 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
688690
| PredicateFilter::SelfOnly
689691
| PredicateFilter::SelfTraitThatDefines(_)
690692
| PredicateFilter::SelfAndAssociatedTypeBounds => {
691-
icx.lowerer().add_implicit_sizedness_bounds(
692-
&mut bounds,
693-
self_param_ty,
694-
superbounds,
695-
ImpliedBoundsContext::TraitDef(trait_def_id),
696-
item.span,
697-
);
698-
icx.lowerer().add_default_traits(
693+
icx.lowerer().add_implicit_bounds(
699694
&mut bounds,
700695
self_param_ty,
701696
superbounds,
702697
ImpliedBoundsContext::TraitDef(trait_def_id),
703698
item.span,
699+
true,
704700
);
705701
}
706702
//`ConstIfConst` is only interested in `[const]` bounds.
@@ -990,19 +986,13 @@ impl<'tcx> ItemCtxt<'tcx> {
990986
match param.kind {
991987
hir::GenericParamKind::Type { .. } => {
992988
let param_ty = self.lowerer().lower_ty_param(param.hir_id);
993-
self.lowerer().add_implicit_sizedness_bounds(
994-
&mut bounds,
995-
param_ty,
996-
&[],
997-
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
998-
param.span,
999-
);
1000-
self.lowerer().add_default_traits(
989+
self.lowerer().add_implicit_bounds(
1001990
&mut bounds,
1002991
param_ty,
1003992
&[],
1004993
ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
1005994
param.span,
995+
true,
1006996
);
1007997
}
1008998
hir::GenericParamKind::Lifetime { .. }

0 commit comments

Comments
 (0)