Skip to content

Commit edd6721

Browse files
authored
Rollup merge of #145095 - tiif:unstable_const_param, r=BoxyUwU
Migrate `UnsizedConstParamTy` to unstable impl of `ConstParamTy_` Now that we have ``#[unstable_feature_bound]``, we can remove ``UnsizedConstParamTy`` that was meant to be an unstable impl of stable type and ``ConstParamTy_`` trait. r? `@BoxyUwU`
2 parents a454fcc + 7299e8f commit edd6721

53 files changed

Lines changed: 266 additions & 405 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_builtin_macros/src/deriving/bounds.rs‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,43 +51,4 @@ pub(crate) fn expand_deriving_const_param_ty(
5151
};
5252

5353
trait_def.expand(cx, mitem, item, push);
54-
55-
let trait_def = TraitDef{
56-
span,
57-
path:path_std!(marker::UnsizedConstParamTy),
58-
skip_path_as_bound:false,
59-
needs_copy_as_bound_if_packed:false,
60-
additional_bounds:vec![ty::Ty::Path(path_std!(cmp::Eq))],
61-
supports_unions:false,
62-
methods:Vec::new(),
63-
associated_types:Vec::new(),
64-
is_const,
65-
is_staged_api_crate: cx.ecfg.features.staged_api(),
66-
};
67-
68-
trait_def.expand(cx, mitem, item, push);
69-
}
70-
71-
pub(crate)fnexpand_deriving_unsized_const_param_ty(
72-
cx:&ExtCtxt<'_>,
73-
span:Span,
74-
mitem:&MetaItem,
75-
item:&Annotatable,
76-
push:&mutdynFnMut(Annotatable),
77-
is_const:bool,
78-
){
79-
let trait_def = TraitDef{
80-
span,
81-
path:path_std!(marker::UnsizedConstParamTy),
82-
skip_path_as_bound:false,
83-
needs_copy_as_bound_if_packed:false,
84-
additional_bounds:vec![ty::Ty::Path(path_std!(cmp::Eq))],
85-
supports_unions:false,
86-
methods:Vec::new(),
87-
associated_types:Vec::new(),
88-
is_const,
89-
is_staged_api_crate: cx.ecfg.features.staged_api(),
90-
};
91-
92-
trait_def.expand(cx, mitem, item, push);
9354
}

‎compiler/rustc_builtin_macros/src/lib.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
129129
Clone: clone::expand_deriving_clone,
130130
Copy: bounds::expand_deriving_copy,
131131
ConstParamTy: bounds::expand_deriving_const_param_ty,
132-
UnsizedConstParamTy: bounds::expand_deriving_unsized_const_param_ty,
133132
Debug: debug::expand_deriving_debug,
134133
Default:default::expand_deriving_default,
135134
Eq: eq::expand_deriving_eq,

‎compiler/rustc_hir/src/lang_items.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ language_item_table! {
370370
CoercePointeeValidated, sym::coerce_pointee_validated, coerce_pointee_validated_trait,Target::Trait,GenericRequirement::Exact(0);
371371

372372
ConstParamTy, sym::const_param_ty, const_param_ty_trait,Target::Trait,GenericRequirement::Exact(0);
373-
UnsizedConstParamTy, sym::unsized_const_param_ty, unsized_const_param_ty_trait,Target::Trait,GenericRequirement::Exact(0);
374373

375374
Poll, sym::Poll, poll,Target::Enum,GenericRequirement::None;
376375
PollReady, sym::Ready, poll_ready_variant,Target::Variant,GenericRequirement::None;

‎compiler/rustc_hir_analysis/src/check/wfcheck.rs‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -819,17 +819,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), Er
819819
let span = tcx.def_span(param.def_id);
820820
let def_id = param.def_id.expect_local();
821821

822-
if tcx.features().unsized_const_params(){
823-
enter_wf_checking_ctxt(tcx, tcx.local_parent(def_id), |wfcx| {
824-
wfcx.register_bound(
825-
ObligationCause::new(span, def_id,ObligationCauseCode::ConstParam(ty)),
826-
wfcx.param_env,
827-
ty,
828-
tcx.require_lang_item(LangItem::UnsizedConstParamTy, span),
829-
);
830-
Ok(())
831-
})
832-
}elseif tcx.features().adt_const_params(){
822+
if tcx.features().adt_const_params(){
833823
enter_wf_checking_ctxt(tcx, tcx.local_parent(def_id), |wfcx| {
834824
wfcx.register_bound(
835825
ObligationCause::new(span, def_id,ObligationCauseCode::ConstParam(ty)),
@@ -880,7 +870,6 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), Er
880870
tcx,
881871
tcx.param_env(param.def_id),
882872
ty,
883-
LangItem::ConstParamTy,
884873
cause,
885874
){
886875
// Can never implement `ConstParamTy`, don't suggest anything.

‎compiler/rustc_hir_analysis/src/coherence/builtin.rs‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Check properties that are required by built-in traits and set
22
//! up data structures required by type-checking/codegen.
33
4-
use std::assert_matches::assert_matches;
54
use std::collections::BTreeMap;
65

76
use rustc_data_structures::fx::FxHashSet;
@@ -40,10 +39,7 @@ pub(super) fn check_trait<'tcx>(
4039
checker.check(lang_items.async_drop_trait(), visit_implementation_of_drop)?;
4140
checker.check(lang_items.copy_trait(), visit_implementation_of_copy)?;
4241
checker.check(lang_items.const_param_ty_trait(), |checker| {
43-
visit_implementation_of_const_param_ty(checker,LangItem::ConstParamTy)
44-
})?;
45-
checker.check(lang_items.unsized_const_param_ty_trait(), |checker| {
46-
visit_implementation_of_const_param_ty(checker,LangItem::UnsizedConstParamTy)
42+
visit_implementation_of_const_param_ty(checker)
4743
})?;
4844
checker.check(lang_items.coerce_unsized_trait(), visit_implementation_of_coerce_unsized)?;
4945
checker
@@ -138,12 +134,7 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran
138134
}
139135
}
140136

141-
fnvisit_implementation_of_const_param_ty(
142-
checker:&Checker<'_>,
143-
kind:LangItem,
144-
) -> Result<(),ErrorGuaranteed>{
145-
assert_matches!(kind,LangItem::ConstParamTy | LangItem::UnsizedConstParamTy);
146-
137+
fnvisit_implementation_of_const_param_ty(checker:&Checker<'_>) -> Result<(),ErrorGuaranteed>{
147138
let tcx = checker.tcx;
148139
let header = checker.impl_header;
149140
let impl_did = checker.impl_def_id;
@@ -157,7 +148,7 @@ fn visit_implementation_of_const_param_ty(
157148
}
158149

159150
let cause = traits::ObligationCause::misc(DUMMY_SP, impl_did);
160-
matchtype_allowed_to_implement_const_param_ty(tcx, param_env, self_type,kind,cause){
151+
matchtype_allowed_to_implement_const_param_ty(tcx, param_env, self_type, cause){
161152
Ok(()) => Ok(()),
162153
Err(ConstParamTyImplementationError::InfrigingFields(fields)) => {
163154
let span = tcx.hir_expect_item(impl_did).expect_impl().self_ty.span;

‎compiler/rustc_infer/src/traits/engine.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,27 @@ pub trait TraitEngine<'tcx, E: 'tcx>: 'tcx {
7272
self.register_predicate_obligation(infcx, obligation);
7373
}
7474
}
75-
75+
/// Go over the list of pending obligations and try to evaluate them.
76+
///
77+
/// For each result:
78+
/// Ok: remove the obligation from the list
79+
/// Ambiguous: leave the obligation in the list to be evaluated later
80+
/// Err: remove the obligation from the list and return an error
81+
///
82+
/// Returns a list of errors from obligations that evaluated to Err.
7683
#[must_use]
7784
fnselect_where_possible(&mutself,infcx:&InferCtxt<'tcx>) -> Vec<E>;
7885

7986
fncollect_remaining_errors(&mutself,infcx:&InferCtxt<'tcx>) -> Vec<E>;
8087

88+
/// Evaluate all pending obligations, return error if they can't be evaluated.
89+
///
90+
/// For each result:
91+
/// Ok: remove the obligation from the list
92+
/// Ambiguous: remove the obligation from the list and return an error
93+
/// Err: remove the obligation from the list and return an error
94+
///
95+
/// Returns a list of errors from obligations that evaluated to Ambiguous or Err.
8196
#[must_use]
8297
fnselect_all_or_error(&mutself,infcx:&InferCtxt<'tcx>) -> Vec<E>{
8398
let errors = self.select_where_possible(infcx);

‎compiler/rustc_trait_selection/src/traits/misc.rs‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
3-
use std::assert_matches::assert_matches;
4-
53
use hir::LangItem;
64
use rustc_ast::Mutability;
75
use rustc_hir as hir;
86
use rustc_infer::infer::{RegionResolutionError,TyCtxtInferExt};
97
use rustc_middle::ty::{self,AdtDef,Ty,TyCtxt,TypeVisitableExt,TypingMode};
8+
use rustc_span::sym;
109

1110
usecrate::regions::InferCtxtRegionExt;
12-
usecrate::traits::{self,FulfillmentError,ObligationCause};
11+
usecrate::traits::{self,FulfillmentError,Obligation,ObligationCause};
1312

1413
pubenumCopyImplementationError<'tcx>{
1514
InfringingFields(Vec<(&'tcx ty::FieldDef,Ty<'tcx>,InfringingFieldsReason<'tcx>)>),
@@ -98,10 +97,9 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>(
9897
tcx:TyCtxt<'tcx>,
9998
param_env: ty::ParamEnv<'tcx>,
10099
self_type:Ty<'tcx>,
101-
lang_item:LangItem,
102100
parent_cause:ObligationCause<'tcx>,
103101
) -> Result<(),ConstParamTyImplementationError<'tcx>>{
104-
assert_matches!(lang_item,LangItem::ConstParamTy | LangItem::UnsizedConstParamTy);
102+
letmut need_unstable_feature_bound = false;
105103

106104
let inner_tys:Vec<_> = match*self_type.kind(){
107105
// Trivially okay as these types are all:
@@ -112,18 +110,14 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>(
112110

113111
// Handle types gated under `feature(unsized_const_params)`
114112
// FIXME(unsized_const_params): Make `const N: [u8]` work then forbid references
115-
ty::Slice(inner_ty) | ty::Ref(_, inner_ty,Mutability::Not)
116-
if lang_item == LangItem::UnsizedConstParamTy =>
117-
{
113+
ty::Slice(inner_ty) | ty::Ref(_, inner_ty,Mutability::Not) => {
114+
need_unstable_feature_bound = true;
118115
vec![inner_ty]
119116
}
120-
ty::Strif lang_item == LangItem::UnsizedConstParamTy => {
117+
ty::Str => {
118+
need_unstable_feature_bound = true;
121119
vec![Ty::new_slice(tcx, tcx.types.u8)]
122120
}
123-
ty::Str | ty::Slice(..) | ty::Ref(_, _,Mutability::Not) => {
124-
returnErr(ConstParamTyImplementationError::UnsizedConstParamsFeatureRequired);
125-
}
126-
127121
ty::Array(inner_ty, _) => vec![inner_ty],
128122

129123
// `str` morally acts like a newtype around `[u8]`
@@ -137,7 +131,7 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>(
137131
adt,
138132
args,
139133
parent_cause.clone(),
140-
lang_item,
134+
LangItem::ConstParamTy,
141135
)
142136
.map_err(ConstParamTyImplementationError::InfrigingFields)?;
143137

@@ -153,11 +147,25 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>(
153147
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
154148
let ocx = traits::ObligationCtxt::new_with_diagnostics(&infcx);
155149

150+
// Make sure impls certain types are gated with #[unstable_feature_bound(unsized_const_params)]
151+
if need_unstable_feature_bound {
152+
ocx.register_obligation(Obligation::new(
153+
tcx,
154+
parent_cause.clone(),
155+
param_env,
156+
ty::ClauseKind::UnstableFeature(sym::unsized_const_params),
157+
));
158+
159+
if !ocx.select_all_or_error().is_empty(){
160+
returnErr(ConstParamTyImplementationError::UnsizedConstParamsFeatureRequired);
161+
}
162+
}
163+
156164
ocx.register_bound(
157165
parent_cause.clone(),
158166
param_env,
159167
inner_ty,
160-
tcx.require_lang_item(lang_item, parent_cause.span),
168+
tcx.require_lang_item(LangItem::ConstParamTy, parent_cause.span),
161169
);
162170

163171
let errors = ocx.select_all_or_error();

‎library/core/src/marker.rs‎

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ pub trait Tuple {}
10831083
// We name this differently than the derive macro so that the `adt_const_params` can
10841084
// be used independently of `unsized_const_params` without requiring a full path
10851085
// to the derive macro every time it is used. This should be renamed on stabilization.
1086-
pubtraitConstParamTy_:UnsizedConstParamTy + StructuralPartialEq + Eq{}
1086+
pubtraitConstParamTy_:StructuralPartialEq + Eq{}
10871087

10881088
/// Derive macro generating an impl of the trait `ConstParamTy`.
10891089
#[rustc_builtin_macro]
@@ -1093,23 +1093,6 @@ pub macro ConstParamTy($item:item) {
10931093
/* compiler built-in */
10941094
}
10951095

1096-
#[lang = "unsized_const_param_ty"]
1097-
#[unstable(feature = "unsized_const_params", issue = "95174")]
1098-
#[diagnostic::on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
1099-
/// A marker for types which can be used as types of `const` generic parameters.
1100-
///
1101-
/// Equivalent to [`ConstParamTy_`] except that this is used by
1102-
/// the `unsized_const_params` to allow for fake unstable impls.
1103-
pubtraitUnsizedConstParamTy:StructuralPartialEq + Eq{}
1104-
1105-
/// Derive macro generating an impl of the trait `ConstParamTy`.
1106-
#[rustc_builtin_macro]
1107-
#[allow_internal_unstable(unsized_const_params)]
1108-
#[unstable(feature = "unsized_const_params", issue = "95174")]
1109-
pub macro UnsizedConstParamTy($item:item){
1110-
/* compiler built-in */
1111-
}
1112-
11131096
// FIXME(adt_const_params): handle `ty::FnDef`/`ty::Closure`
11141097
marker_impls!{
11151098
#[unstable(feature = "adt_const_params", issue = "95174")]
@@ -1124,17 +1107,11 @@ marker_impls! {
11241107

11251108
marker_impls!{
11261109
#[unstable(feature = "unsized_const_params", issue = "95174")]
1127-
UnsizedConstParamTyfor
1128-
usize,u8,u16,u32,u64,u128,
1129-
isize,i8,i16,i32,i64,i128,
1130-
bool,
1131-
char,
1132-
(),
1133-
{T:UnsizedConstParamTy,constN:usize}[T;N],
1134-
1110+
#[unstable_feature_bound(unsized_const_params)]
1111+
ConstParamTy_for
11351112
str,
1136-
{T:UnsizedConstParamTy}[T],
1137-
{T:UnsizedConstParamTy + ?Sized}&T,
1113+
{T:ConstParamTy_}[T],
1114+
{T:ConstParamTy_ + ?Sized}&T,
11381115
}
11391116

11401117
/// A common trait implemented by all function pointers.

‎library/core/src/mem/transmutability.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usecrate::marker::{ConstParamTy_,UnsizedConstParamTy};
1+
usecrate::marker::ConstParamTy_;
22

33
/// Marks that `Src` is transmutable into `Self`.
44
///
@@ -83,6 +83,7 @@ use crate::marker::{ConstParamTy_, UnsizedConstParamTy};
8383
/// Furthermore, stability does not imply portability. For example, the size of
8484
/// `usize` is stable, but not portable.
8585
#[unstable(feature = "transmutability", issue = "99571")]
86+
#[unstable_feature_bound(transmutability)]
8687
#[lang = "transmute_trait"]
8788
#[rustc_deny_explicit_impl]
8889
#[rustc_do_not_implement_via_object]
@@ -288,9 +289,8 @@ pub struct Assume {
288289
}
289290

290291
#[unstable(feature = "transmutability", issue = "99571")]
292+
#[unstable_feature_bound(transmutability)]
291293
implConstParamTy_forAssume{}
292-
#[unstable(feature = "transmutability", issue = "99571")]
293-
implUnsizedConstParamTyforAssume{}
294294

295295
implAssume{
296296
/// With this, [`TransmuteFrom`] does not assume you have ensured any safety

‎library/core/src/tuple.rs‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// See core/src/primitive_docs.rs for documentation.
22

33
usecrate::cmp::Ordering::{self,*};
4-
usecrate::marker::{ConstParamTy_,StructuralPartialEq,UnsizedConstParamTy};
4+
usecrate::marker::{ConstParamTy_,StructuralPartialEq};
55
usecrate::ops::ControlFlow::{self,Break,Continue};
66

77
// Recursive macro for implementing n-ary tuple functions and operations
@@ -47,17 +47,11 @@ macro_rules! tuple_impls {
4747
maybe_tuple_doc! {
4848
$($T)+ @
4949
#[unstable(feature = "adt_const_params", issue = "95174")]
50+
#[unstable_feature_bound(unsized_const_params)]
5051
impl<$($T:ConstParamTy_),+> ConstParamTy_for($($T,)+)
5152
{}
5253
}
5354

54-
maybe_tuple_doc! {
55-
$($T)+ @
56-
#[unstable(feature = "unsized_const_params", issue = "95174")]
57-
impl<$($T:UnsizedConstParamTy),+> UnsizedConstParamTyfor($($T,)+)
58-
{}
59-
}
60-
6155
maybe_tuple_doc! {
6256
$($T)+ @
6357
#[unstable(feature = "structural_match", issue = "31434")]

0 commit comments

Comments
 (0)