Skip to content

Commit d9ec06f

Browse files
committed
Change is_unsized to add_implicitly_sized
1 parent 81aa120 commit d9ec06f

File tree

4 files changed

+62
-122
lines changed

4 files changed

+62
-122
lines changed

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13681368
// Error if `?Trait` bounds in where clauses don't refer directly to type paramters.
13691369
// Note: we used to clone these bounds directly onto the type parameter (and avoid lowering
13701370
// these into hir when we lower thee where clauses), but this makes it quite difficult to
1371-
// keep track of the Span info. Now, `is_unsized` in `AstConv` checks both param bounds and
1371+
// keep track of the Span info. Now, `add_implicitly_sized` in `AstConv` checks both param bounds and
13721372
// where clauses for `?Sized`.
13731373
for pred in &generics.where_clause.predicates {
13741374
if let WherePredicate::BoundPredicate(ref bound_pred) = *pred {

compiler/rustc_typeck/src/astconv/mod.rs

+42-79
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,6 @@ pub trait AstConv<'tcx> {
113113
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
114114
}
115115

116-
pub enum SizedByDefault {
117-
Yes,
118-
No,
119-
}
120-
121116
#[derive(Debug)]
122117
struct ConvertedBinding<'a, 'tcx> {
123118
hir_id: hir::HirId,
@@ -856,27 +851,30 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
856851
}
857852

858853
// Returns `true` if a bounds list includes `?Sized`.
859-
fn is_unsized(
854+
pub(crate) fn add_implicitly_sized<'hir>(
860855
&self,
861-
ast_bounds: &[hir::GenericBound<'_>],
862-
self_ty: Option<hir::HirId>,
863-
where_clause: Option<&[hir::WherePredicate<'_>]>,
856+
bounds: &mut Bounds<'hir>,
857+
ast_bounds: &'hir [hir::GenericBound<'hir>],
858+
self_ty_where_predicates: Option<(hir::HirId, &'hir [hir::WherePredicate<'hir>])>,
864859
span: Span,
865-
) -> bool {
860+
) {
866861
let tcx = self.tcx();
867862

868863
// Try to find an unbound in bounds.
869864
let mut unbound = None;
870-
for ab in ast_bounds {
871-
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
872-
if unbound.is_none() {
873-
unbound = Some(&ptr.trait_ref);
874-
} else {
875-
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
865+
let mut search_bounds = |ast_bounds: &'hir [hir::GenericBound<'hir>]| {
866+
for ab in ast_bounds {
867+
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
868+
if unbound.is_none() {
869+
unbound = Some(&ptr.trait_ref);
870+
} else {
871+
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
872+
}
876873
}
877874
}
878-
}
879-
if let (Some(self_ty), Some(where_clause)) = (self_ty, where_clause) {
875+
};
876+
search_bounds(ast_bounds);
877+
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
880878
let self_ty_def_id = tcx.hir().local_def_id(self_ty).to_def_id();
881879
for clause in where_clause {
882880
match clause {
@@ -888,46 +886,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
888886
},
889887
_ => continue,
890888
}
891-
for ab in pred.bounds {
892-
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) =
893-
ab
894-
{
895-
if unbound.is_none() {
896-
unbound = Some(&ptr.trait_ref);
897-
} else {
898-
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
899-
}
900-
}
901-
}
889+
search_bounds(pred.bounds);
902890
}
903891
_ => {}
904892
}
905893
}
906894
}
907895

908-
let kind_id = tcx.lang_items().require(LangItem::Sized);
909-
match unbound {
910-
Some(tpb) => {
911-
if let Ok(kind_id) = kind_id {
912-
if tpb.path.res != Res::Def(DefKind::Trait, kind_id) {
913-
tcx.sess.span_warn(
914-
span,
915-
"default bound relaxed for a type parameter, but \
916-
this does nothing because the given bound is not \
917-
a default; only `?Sized` is supported",
918-
);
919-
return false;
920-
}
921-
}
896+
let sized_def_id = tcx.lang_items().require(LangItem::Sized);
897+
match (&sized_def_id, unbound) {
898+
(Ok(sized_def_id), Some(tpb))
899+
if tpb.path.res == Res::Def(DefKind::Trait, *sized_def_id) =>
900+
{
901+
// There was in fact a `?Sized` bound, return without doing anything
902+
return;
922903
}
923-
_ if kind_id.is_ok() => {
924-
return false;
904+
(_, Some(_)) => {
905+
// There was a `?Trait` bound, but it was not `?Sized`; warn.
906+
tcx.sess.span_warn(
907+
span,
908+
"default bound relaxed for a type parameter, but \
909+
this does nothing because the given bound is not \
910+
a default; only `?Sized` is supported",
911+
);
912+
// Otherwise, add implicitly sized if `Sized` is available.
913+
}
914+
_ => {
915+
// There was no `?Sized` bound; add implicitly sized if `Sized` is available.
925916
}
917+
}
918+
if sized_def_id.is_err() {
926919
// No lang item for `Sized`, so we can't add it as a bound.
927-
None => {}
920+
return;
928921
}
929-
930-
true
922+
bounds.implicitly_sized = Some(span);
931923
}
932924

933925
/// This helper takes a *converted* parameter type (`param_ty`)
@@ -1009,19 +1001,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10091001
&self,
10101002
param_ty: Ty<'tcx>,
10111003
ast_bounds: &[hir::GenericBound<'_>],
1012-
self_ty: Option<hir::HirId>,
1013-
where_clause: Option<&[hir::WherePredicate<'_>]>,
1014-
sized_by_default: SizedByDefault,
1015-
span: Span,
10161004
) -> Bounds<'tcx> {
1017-
self.compute_bounds_inner(
1018-
param_ty,
1019-
&ast_bounds,
1020-
self_ty,
1021-
where_clause,
1022-
sized_by_default,
1023-
span,
1024-
)
1005+
self.compute_bounds_inner(param_ty, &ast_bounds)
10251006
}
10261007

10271008
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
@@ -1030,10 +1011,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10301011
&self,
10311012
param_ty: Ty<'tcx>,
10321013
ast_bounds: &[hir::GenericBound<'_>],
1033-
self_ty: Option<hir::HirId>,
1034-
where_clause: Option<&[hir::WherePredicate<'_>]>,
1035-
sized_by_default: SizedByDefault,
1036-
span: Span,
10371014
assoc_name: Ident,
10381015
) -> Bounds<'tcx> {
10391016
let mut result = Vec::new();
@@ -1048,32 +1025,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10481025
}
10491026
}
10501027

1051-
self.compute_bounds_inner(param_ty, &result, self_ty, where_clause, sized_by_default, span)
1028+
self.compute_bounds_inner(param_ty, &result)
10521029
}
10531030

10541031
fn compute_bounds_inner(
10551032
&self,
10561033
param_ty: Ty<'tcx>,
10571034
ast_bounds: &[hir::GenericBound<'_>],
1058-
self_ty: Option<hir::HirId>,
1059-
where_clause: Option<&[hir::WherePredicate<'_>]>,
1060-
sized_by_default: SizedByDefault,
1061-
span: Span,
10621035
) -> Bounds<'tcx> {
10631036
let mut bounds = Bounds::default();
10641037

10651038
self.add_bounds(param_ty, ast_bounds, &mut bounds, ty::List::empty());
10661039

1067-
bounds.implicitly_sized = if let SizedByDefault::Yes = sized_by_default {
1068-
if !self.is_unsized(ast_bounds, self_ty, where_clause, span) {
1069-
Some(span)
1070-
} else {
1071-
None
1072-
}
1073-
} else {
1074-
None
1075-
};
1076-
10771040
bounds
10781041
}
10791042

compiler/rustc_typeck/src/collect.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! At present, however, we do run collection across all items in the
1515
//! crate as a kind of pass. This should eventually be factored away.
1616
17-
use crate::astconv::{AstConv, SizedByDefault};
17+
use crate::astconv::AstConv;
1818
use crate::bounds::Bounds;
1919
use crate::check::intrinsic::intrinsic_operation_unsafety;
2020
use crate::constrained_generic_params as cgp;
@@ -1158,22 +1158,10 @@ fn super_predicates_that_define_assoc_type(
11581158
&icx,
11591159
self_param_ty,
11601160
&bounds,
1161-
None,
1162-
None,
1163-
SizedByDefault::No,
1164-
item.span,
11651161
assoc_name,
11661162
)
11671163
} else {
1168-
<dyn AstConv<'_>>::compute_bounds(
1169-
&icx,
1170-
self_param_ty,
1171-
&bounds,
1172-
None,
1173-
None,
1174-
SizedByDefault::No,
1175-
item.span,
1176-
)
1164+
<dyn AstConv<'_>>::compute_bounds(&icx, self_param_ty, &bounds)
11771165
};
11781166

11791167
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
@@ -2174,14 +2162,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
21742162
let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
21752163
index += 1;
21762164

2177-
let sized = SizedByDefault::Yes;
2178-
let bounds = <dyn AstConv<'_>>::compute_bounds(
2165+
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, param_ty, &param.bounds);
2166+
// Params are implicitly sized unless a `?Sized` bound is found
2167+
<dyn AstConv<'_>>::add_implicitly_sized(
21792168
&icx,
2180-
param_ty,
2169+
&mut bounds,
21812170
&param.bounds,
2182-
Some(param.hir_id),
2183-
Some(ast_generics.where_clause.predicates),
2184-
sized,
2171+
Some((param.hir_id, ast_generics.where_clause.predicates)),
21852172
param.span,
21862173
);
21872174
predicates.extend(bounds.predicates(tcx, param_ty));

compiler/rustc_typeck/src/collect/item_bounds.rs

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::ItemCtxt;
2-
use crate::astconv::{AstConv, SizedByDefault};
2+
use crate::astconv::AstConv;
33
use rustc_hir as hir;
44
use rustc_infer::traits::util;
55
use rustc_middle::ty::subst::InternalSubsts;
@@ -17,23 +17,18 @@ use rustc_span::Span;
1717
fn associated_type_bounds<'tcx>(
1818
tcx: TyCtxt<'tcx>,
1919
assoc_item_def_id: DefId,
20-
bounds: &'tcx [hir::GenericBound<'tcx>],
20+
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
2121
span: Span,
2222
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
2323
let item_ty = tcx.mk_projection(
2424
assoc_item_def_id,
2525
InternalSubsts::identity_for_item(tcx, assoc_item_def_id),
2626
);
2727

28-
let bounds = <dyn AstConv<'_>>::compute_bounds(
29-
&ItemCtxt::new(tcx, assoc_item_def_id),
30-
item_ty,
31-
&bounds,
32-
None,
33-
None,
34-
SizedByDefault::Yes,
35-
span,
36-
);
28+
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
29+
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, item_ty, &ast_bounds);
30+
// Associated types are implicitly sized unless a `?Sized` bound is found
31+
<dyn AstConv<'_>>::add_implicitly_sized(&icx, &mut bounds, &ast_bounds, None, span);
3732

3833
let trait_def_id = tcx.associated_item(assoc_item_def_id).container.id();
3934
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id.expect_local());
@@ -61,23 +56,18 @@ fn associated_type_bounds<'tcx>(
6156
fn opaque_type_bounds<'tcx>(
6257
tcx: TyCtxt<'tcx>,
6358
opaque_def_id: DefId,
64-
bounds: &'tcx [hir::GenericBound<'tcx>],
59+
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
6560
span: Span,
6661
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
6762
ty::print::with_no_queries(|| {
6863
let item_ty =
6964
tcx.mk_opaque(opaque_def_id, InternalSubsts::identity_for_item(tcx, opaque_def_id));
7065

71-
let bounds = <dyn AstConv<'_>>::compute_bounds(
72-
&ItemCtxt::new(tcx, opaque_def_id),
73-
item_ty,
74-
&bounds,
75-
None,
76-
None,
77-
SizedByDefault::Yes,
78-
span,
79-
)
80-
.predicates(tcx, item_ty);
66+
let icx = ItemCtxt::new(tcx, opaque_def_id);
67+
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, item_ty, &ast_bounds);
68+
// Opaque types are implicitly sized unless a `?Sized` bound is found
69+
<dyn AstConv<'_>>::add_implicitly_sized(&icx, &mut bounds, &ast_bounds, None, span);
70+
let bounds = bounds.predicates(tcx, item_ty);
8171

8272
debug!("opaque_type_bounds({}) = {:?}", tcx.def_path_str(opaque_def_id), bounds);
8373

0 commit comments

Comments
 (0)