Skip to content

Commit 216906f

Browse files
committed
Change is_unsized to add_implicitly_sized
1 parent f1f1d56 commit 216906f

File tree

4 files changed

+63
-123
lines changed

4 files changed

+63
-123
lines changed

compiler/rustc_ast_lowering/src/item.rs

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

compiler/rustc_typeck/src/astconv/mod.rs

+43-80
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ pub trait AstConv<'tcx> {
111111
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
112112
}
113113

114-
pub enum SizedByDefault {
115-
Yes,
116-
No,
117-
}
118-
119114
#[derive(Debug)]
120115
struct ConvertedBinding<'a, 'tcx> {
121116
hir_id: hir::HirId,
@@ -853,28 +848,31 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
853848
.is_some()
854849
}
855850

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

866861
// Try to find an unbound in bounds.
867862
let mut unbound = None;
868-
for ab in ast_bounds {
869-
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
870-
if unbound.is_none() {
871-
unbound = Some(&ptr.trait_ref);
872-
} else {
873-
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
863+
let mut search_bounds = |ast_bounds: &'hir [hir::GenericBound<'hir>]| {
864+
for ab in ast_bounds {
865+
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
866+
if unbound.is_none() {
867+
unbound = Some(&ptr.trait_ref);
868+
} else {
869+
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
870+
}
874871
}
875872
}
876-
}
877-
if let (Some(self_ty), Some(where_clause)) = (self_ty, where_clause) {
873+
};
874+
search_bounds(ast_bounds);
875+
if let Some((self_ty, where_clause)) = self_ty_where_predicates {
878876
let self_ty_def_id = tcx.hir().local_def_id(self_ty).to_def_id();
879877
for clause in where_clause {
880878
match clause {
@@ -886,46 +884,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
886884
},
887885
_ => continue,
888886
}
889-
for ab in pred.bounds {
890-
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) =
891-
ab
892-
{
893-
if unbound.is_none() {
894-
unbound = Some(&ptr.trait_ref);
895-
} else {
896-
tcx.sess.emit_err(MultipleRelaxedDefaultBounds { span });
897-
}
898-
}
899-
}
887+
search_bounds(pred.bounds);
900888
}
901889
_ => {}
902890
}
903891
}
904892
}
905893

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

931923
/// This helper takes a *converted* parameter type (`param_ty`)
@@ -1006,19 +998,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1006998
&self,
1007999
param_ty: Ty<'tcx>,
10081000
ast_bounds: &[hir::GenericBound<'_>],
1009-
self_ty: Option<hir::HirId>,
1010-
where_clause: Option<&[hir::WherePredicate<'_>]>,
1011-
sized_by_default: SizedByDefault,
1012-
span: Span,
10131001
) -> Bounds<'tcx> {
1014-
self.compute_bounds_inner(
1015-
param_ty,
1016-
&ast_bounds,
1017-
self_ty,
1018-
where_clause,
1019-
sized_by_default,
1020-
span,
1021-
)
1002+
self.compute_bounds_inner(param_ty, &ast_bounds)
10221003
}
10231004

10241005
/// Convert the bounds in `ast_bounds` that refer to traits which define an associated type
@@ -1027,10 +1008,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10271008
&self,
10281009
param_ty: Ty<'tcx>,
10291010
ast_bounds: &[hir::GenericBound<'_>],
1030-
self_ty: Option<hir::HirId>,
1031-
where_clause: Option<&[hir::WherePredicate<'_>]>,
1032-
sized_by_default: SizedByDefault,
1033-
span: Span,
10341011
assoc_name: Ident,
10351012
) -> Bounds<'tcx> {
10361013
let mut result = Vec::new();
@@ -1045,32 +1022,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10451022
}
10461023
}
10471024

1048-
self.compute_bounds_inner(param_ty, &result, self_ty, where_clause, sized_by_default, span)
1025+
self.compute_bounds_inner(param_ty, &result)
10491026
}
10501027

10511028
fn compute_bounds_inner(
10521029
&self,
10531030
param_ty: Ty<'tcx>,
10541031
ast_bounds: &[hir::GenericBound<'_>],
1055-
self_ty: Option<hir::HirId>,
1056-
where_clause: Option<&[hir::WherePredicate<'_>]>,
1057-
sized_by_default: SizedByDefault,
1058-
span: Span,
10591032
) -> Bounds<'tcx> {
10601033
let mut bounds = Bounds::default();
10611034

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

1064-
bounds.implicitly_sized = if let SizedByDefault::Yes = sized_by_default {
1065-
if !self.is_unsized(ast_bounds, self_ty, where_clause, span) {
1066-
Some(span)
1067-
} else {
1068-
None
1069-
}
1070-
} else {
1071-
None
1072-
};
1073-
10741037
bounds
10751038
}
10761039

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;
@@ -1156,22 +1156,10 @@ fn super_predicates_that_define_assoc_type(
11561156
&icx,
11571157
self_param_ty,
11581158
&bounds,
1159-
None,
1160-
None,
1161-
SizedByDefault::No,
1162-
item.span,
11631159
assoc_name,
11641160
)
11651161
} else {
1166-
<dyn AstConv<'_>>::compute_bounds(
1167-
&icx,
1168-
self_param_ty,
1169-
&bounds,
1170-
None,
1171-
None,
1172-
SizedByDefault::No,
1173-
item.span,
1174-
)
1162+
<dyn AstConv<'_>>::compute_bounds(&icx, self_param_ty, &bounds)
11751163
};
11761164

11771165
let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
@@ -2180,14 +2168,13 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
21802168
let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
21812169
index += 1;
21822170

2183-
let sized = SizedByDefault::Yes;
2184-
let bounds = <dyn AstConv<'_>>::compute_bounds(
2171+
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, param_ty, &param.bounds);
2172+
// Params are implicitly sized unless a `?Sized` bound is found
2173+
<dyn AstConv<'_>>::add_implicitly_sized(
21852174
&icx,
2186-
param_ty,
2175+
&mut bounds,
21872176
&param.bounds,
2188-
Some(param.hir_id),
2189-
Some(ast_generics.where_clause.predicates),
2190-
sized,
2177+
Some((param.hir_id, ast_generics.where_clause.predicates)),
21912178
param.span,
21922179
);
21932180
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)