Skip to content

Commit a167c04

Browse files
committed
Auto merge of #39110 - petrochenkov:sum, r=eddyb
Merge ObjectSum and PolyTraitRef in AST/HIR + some other refactoring `ObjectSum` and `PolyTraitRef` are the same thing (list of bounds), they exist separately only due to parser quirks. The second commit merges them. The first commit replaces `Path` with `Ty` in (not yet supported) equality predicates. They are parsed as types anyway and arbitrary types can always be disguised as paths using aliases, so this doesn't add any new functionality. The third commit uses `Vec` instead of `P<[T]>` in AST. AST is not immutable like HIR and `Vec`s are more convenient for it, unnecessary conversions are also avoided. The last commit renames `parse_ty_sum` (which is used for parsing types in general) into `parse_ty`, and renames `parse_ty` (which is used restricted contexts where `+` is not permitted due to operator priorities or other reasons) into `parse_ty_no_plus`. This is the first part of #39085 (comment) and #39080 focused on data changes and mechanical renaming, I'll submit a PR with parser changes a bit later. r? @eddyb
2 parents 0825c96 + 66ef5f2 commit a167c04

File tree

30 files changed

+239
-373
lines changed

30 files changed

+239
-373
lines changed

src/librustc/hir/intravisit.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,11 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
562562
TyPath(ref qpath) => {
563563
visitor.visit_qpath(qpath, typ.id, typ.span);
564564
}
565-
TyObjectSum(ref ty, ref bounds) => {
566-
visitor.visit_ty(ty);
567-
walk_list!(visitor, visit_ty_param_bound, bounds);
568-
}
569565
TyArray(ref ty, length) => {
570566
visitor.visit_ty(ty);
571567
visitor.visit_nested_body(length)
572568
}
573-
TyPolyTraitRef(ref bounds) => {
569+
TyTraitObject(ref bounds) => {
574570
walk_list!(visitor, visit_ty_param_bound, bounds);
575571
}
576572
TyImplTrait(ref bounds) => {
@@ -740,12 +736,12 @@ pub fn walk_where_predicate<'v, V: Visitor<'v>>(
740736
walk_list!(visitor, visit_lifetime, bounds);
741737
}
742738
&WherePredicate::EqPredicate(WhereEqPredicate{id,
743-
ref path,
744-
ref ty,
739+
ref lhs_ty,
740+
ref rhs_ty,
745741
..}) => {
746742
visitor.visit_id(id);
747-
visitor.visit_path(path, id);
748-
visitor.visit_ty(ty);
743+
visitor.visit_ty(lhs_ty);
744+
visitor.visit_ty(rhs_ty);
749745
}
750746
}
751747
}

src/librustc/hir/lowering.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ impl<'a> LoweringContext<'a> {
308308
span: t.span,
309309
})))
310310
}
311-
TyKind::ObjectSum(ref ty, ref bounds) => {
312-
hir::TyObjectSum(self.lower_ty(ty), self.lower_bounds(bounds))
313-
}
314311
TyKind::Array(ref ty, ref length) => {
315312
let length = self.lower_expr(length);
316313
hir::TyArray(self.lower_ty(ty),
@@ -320,8 +317,8 @@ impl<'a> LoweringContext<'a> {
320317
let expr = self.lower_expr(expr);
321318
hir::TyTypeof(self.record_body(expr, None))
322319
}
323-
TyKind::PolyTraitRef(ref bounds) => {
324-
hir::TyPolyTraitRef(self.lower_bounds(bounds))
320+
TyKind::TraitObject(ref bounds) => {
321+
hir::TyTraitObject(self.lower_bounds(bounds))
325322
}
326323
TyKind::ImplTrait(ref bounds) => {
327324
hir::TyImplTrait(self.lower_bounds(bounds))
@@ -599,7 +596,7 @@ impl<'a> LoweringContext<'a> {
599596
}
600597
}
601598

602-
fn lower_ty_params(&mut self, tps: &P<[TyParam]>, add_bounds: &NodeMap<Vec<TyParamBound>>)
599+
fn lower_ty_params(&mut self, tps: &Vec<TyParam>, add_bounds: &NodeMap<Vec<TyParamBound>>)
603600
-> hir::HirVec<hir::TyParam> {
604601
tps.iter().map(|tp| {
605602
self.lower_ty_param(tp, add_bounds.get(&tp.id).map_or(&[][..], |x| &x))
@@ -719,13 +716,13 @@ impl<'a> LoweringContext<'a> {
719716
})
720717
}
721718
WherePredicate::EqPredicate(WhereEqPredicate{ id,
722-
ref path,
723-
ref ty,
719+
ref lhs_ty,
720+
ref rhs_ty,
724721
span}) => {
725722
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
726723
id: id,
727-
path: self.lower_path(id, path, ParamMode::Explicit, false),
728-
ty: self.lower_ty(ty),
724+
lhs_ty: self.lower_ty(lhs_ty),
725+
rhs_ty: self.lower_ty(rhs_ty),
729726
span: span,
730727
})
731728
}

src/librustc/hir/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ pub struct WhereRegionPredicate {
403403
pub struct WhereEqPredicate {
404404
pub id: NodeId,
405405
pub span: Span,
406-
pub path: Path,
407-
pub ty: P<Ty>,
406+
pub lhs_ty: P<Ty>,
407+
pub rhs_ty: P<Ty>,
408408
}
409409

410410
pub type CrateConfig = HirVec<P<MetaItem>>;
@@ -1214,12 +1214,11 @@ pub enum Ty_ {
12141214
///
12151215
/// Type parameters may be stored in each `PathSegment`.
12161216
TyPath(QPath),
1217-
1218-
/// Something like `A+B`. Note that `B` must always be a path.
1219-
TyObjectSum(P<Ty>, TyParamBounds),
1220-
/// A type like `for<'a> Foo<&'a Bar>`
1221-
TyPolyTraitRef(TyParamBounds),
1222-
/// An `impl TraitA+TraitB` type.
1217+
/// A trait object type `Bound1 + Bound2 + Bound3`
1218+
/// where `Bound` is a trait or a lifetime.
1219+
TyTraitObject(TyParamBounds),
1220+
/// An `impl Bound1 + Bound2 + Bound3` type
1221+
/// where `Bound` is a trait or a lifetime.
12231222
TyImplTrait(TyParamBounds),
12241223
/// Unused for now
12251224
TyTypeof(BodyId),

src/librustc/hir/print.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,7 @@ impl<'a> State<'a> {
418418
hir::TyPath(ref qpath) => {
419419
self.print_qpath(qpath, false)?
420420
}
421-
hir::TyObjectSum(ref ty, ref bounds) => {
422-
self.print_type(&ty)?;
423-
self.print_bounds("+", &bounds[..])?;
424-
}
425-
hir::TyPolyTraitRef(ref bounds) => {
421+
hir::TyTraitObject(ref bounds) => {
426422
self.print_bounds("", &bounds[..])?;
427423
}
428424
hir::TyImplTrait(ref bounds) => {
@@ -2023,11 +2019,13 @@ impl<'a> State<'a> {
20232019
}
20242020
}
20252021
}
2026-
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref path, ref ty, ..}) => {
2027-
self.print_path(path, false)?;
2022+
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref lhs_ty,
2023+
ref rhs_ty,
2024+
..}) => {
2025+
self.print_type(lhs_ty)?;
20282026
space(&mut self.s)?;
20292027
self.word_space("=")?;
2030-
self.print_type(&ty)?;
2028+
self.print_type(rhs_ty)?;
20312029
}
20322030
}
20332031
}

src/librustc/middle/resolve_lifetime.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
323323
self.visit_lifetime(bound);
324324
}
325325
}
326-
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ id,
327-
ref path,
328-
ref ty,
329-
.. }) => {
330-
self.visit_path(path, id);
331-
self.visit_ty(&ty);
326+
&hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{ref lhs_ty,
327+
ref rhs_ty,
328+
.. }) => {
329+
self.visit_ty(lhs_ty);
330+
self.visit_ty(rhs_ty);
332331
}
333332
}
334333
}

src/librustc_incremental/calculate_svh/svh_visitor.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,6 @@ enum SawTyComponent {
441441
SawTyTup,
442442
SawTyPath,
443443
SawTyObjectSum,
444-
SawTyPolyTraitRef,
445444
SawTyImplTrait,
446445
SawTyTypeof,
447446
SawTyInfer
@@ -457,8 +456,7 @@ fn saw_ty(node: &Ty_) -> SawTyComponent {
457456
TyNever => SawTyNever,
458457
TyTup(..) => SawTyTup,
459458
TyPath(_) => SawTyPath,
460-
TyObjectSum(..) => SawTyObjectSum,
461-
TyPolyTraitRef(..) => SawTyPolyTraitRef,
459+
TyTraitObject(..) => SawTyObjectSum,
462460
TyImplTrait(..) => SawTyImplTrait,
463461
TyTypeof(..) => SawTyTypeof,
464462
TyInfer => SawTyInfer

src/librustc_passes/ast_validation.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
143143
err.emit();
144144
});
145145
}
146-
TyKind::ObjectSum(_, ref bounds) |
147-
TyKind::PolyTraitRef(ref bounds) => {
146+
TyKind::TraitObject(ref bounds) => {
148147
self.no_questions_in_bounds(bounds, "trait object types", false);
149148
}
150149
_ => {}

src/librustc_privacy/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
853853
}
854854
&hir::WherePredicate::RegionPredicate(_) => {}
855855
&hir::WherePredicate::EqPredicate(ref eq_pred) => {
856-
self.visit_ty(&eq_pred.ty);
856+
self.visit_ty(&eq_pred.rhs_ty);
857857
}
858858
}
859859
}

src/librustc_typeck/astconv.rs

+3-87
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use std::iter;
7373
use syntax::{abi, ast};
7474
use syntax::feature_gate::{GateIssue, emit_feature_err};
7575
use syntax::symbol::{Symbol, keywords};
76-
use syntax_pos::{Span, Pos};
76+
use syntax_pos::Span;
7777
use errors::DiagnosticBuilder;
7878

7979
pub trait AstConv<'gcx, 'tcx> {
@@ -930,87 +930,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
930930
decl_ty.subst(self.tcx(), substs)
931931
}
932932

933-
fn ast_ty_to_object_trait_ref(&self,
934-
rscope: &RegionScope,
935-
span: Span,
936-
ty: &hir::Ty,
937-
bounds: &[hir::TyParamBound])
938-
-> Ty<'tcx>
939-
{
940-
/*!
941-
* In a type like `Foo + Send`, we want to wait to collect the
942-
* full set of bounds before we make the object type, because we
943-
* need them to infer a region bound. (For example, if we tried
944-
* made a type from just `Foo`, then it wouldn't be enough to
945-
* infer a 'static bound, and hence the user would get an error.)
946-
* So this function is used when we're dealing with a sum type to
947-
* convert the LHS. It only accepts a type that refers to a trait
948-
* name, and reports an error otherwise.
949-
*/
950-
951-
let tcx = self.tcx();
952-
match ty.node {
953-
hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
954-
if let Def::Trait(trait_def_id) = path.def {
955-
self.trait_path_to_object_type(rscope,
956-
path.span,
957-
trait_def_id,
958-
ty.id,
959-
path.segments.last().unwrap(),
960-
span,
961-
partition_bounds(bounds))
962-
} else {
963-
struct_span_err!(tcx.sess, ty.span, E0172,
964-
"expected a reference to a trait")
965-
.span_label(ty.span, &format!("expected a trait"))
966-
.emit();
967-
tcx.types.err
968-
}
969-
}
970-
_ => {
971-
let mut err = struct_span_err!(tcx.sess, ty.span, E0178,
972-
"expected a path on the left-hand side \
973-
of `+`, not `{}`",
974-
tcx.map.node_to_pretty_string(ty.id));
975-
err.span_label(ty.span, &format!("expected a path"));
976-
let hi = bounds.iter().map(|x| match *x {
977-
hir::TraitTyParamBound(ref tr, _) => tr.span.hi,
978-
hir::RegionTyParamBound(ref r) => r.span.hi,
979-
}).max_by_key(|x| x.to_usize());
980-
let full_span = hi.map(|hi| Span {
981-
lo: ty.span.lo,
982-
hi: hi,
983-
expn_id: ty.span.expn_id,
984-
});
985-
match (&ty.node, full_span) {
986-
(&hir::TyRptr(ref lifetime, ref mut_ty), Some(full_span)) => {
987-
let ty_str = hir::print::to_string(&tcx.map, |s| {
988-
use syntax::print::pp::word;
989-
use syntax::print::pprust::PrintState;
990-
991-
word(&mut s.s, "&")?;
992-
s.print_opt_lifetime(lifetime)?;
993-
s.print_mutability(mut_ty.mutbl)?;
994-
s.popen()?;
995-
s.print_type(&mut_ty.ty)?;
996-
s.print_bounds(" +", bounds)?;
997-
s.pclose()
998-
});
999-
err.span_suggestion(full_span, "try adding parentheses (per RFC 438):",
1000-
ty_str);
1001-
}
1002-
1003-
_ => {
1004-
help!(&mut err,
1005-
"perhaps you forgot parentheses? (per RFC 438)");
1006-
}
1007-
}
1008-
err.emit();
1009-
tcx.types.err
1010-
}
1011-
}
1012-
}
1013-
1014933
/// Transform a PolyTraitRef into a PolyExistentialTraitRef by
1015934
/// removing the dummy Self type (TRAIT_OBJECT_DUMMY_SELF).
1016935
fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>)
@@ -1428,7 +1347,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
14281347
match path.def {
14291348
Def::Trait(trait_def_id) => {
14301349
// N.B. this case overlaps somewhat with
1431-
// TyObjectSum, see that fn for details
1350+
// TyTraitObject, see that fn for details
14321351

14331352
assert_eq!(opt_self_ty, None);
14341353
tcx.prohibit_type_params(path.segments.split_last().unwrap().1);
@@ -1534,9 +1453,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
15341453
hir::TySlice(ref ty) => {
15351454
tcx.mk_slice(self.ast_ty_to_ty(rscope, &ty))
15361455
}
1537-
hir::TyObjectSum(ref ty, ref bounds) => {
1538-
self.ast_ty_to_object_trait_ref(rscope, ast_ty.span, ty, bounds)
1539-
}
15401456
hir::TyPtr(ref mt) => {
15411457
tcx.mk_ptr(ty::TypeAndMut {
15421458
ty: self.ast_ty_to_ty(rscope, &mt.ty),
@@ -1609,7 +1525,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
16091525
}
16101526
tcx.mk_fn_ptr(bare_fn_ty)
16111527
}
1612-
hir::TyPolyTraitRef(ref bounds) => {
1528+
hir::TyTraitObject(ref bounds) => {
16131529
self.conv_object_ty_poly_trait_ref(rscope, ast_ty.span, bounds)
16141530
}
16151531
hir::TyImplTrait(ref bounds) => {

src/librustc_typeck/diagnostics.rs

+1-49
Original file line numberDiff line numberDiff line change
@@ -1864,55 +1864,6 @@ fn bar(foo: Foo) -> u32 {
18641864
```
18651865
"##,
18661866

1867-
E0172: r##"
1868-
This error means that an attempt was made to specify the type of a variable with
1869-
a combination of a concrete type and a trait. Consider the following example:
1870-
1871-
```compile_fail,E0172
1872-
fn foo(bar: i32+std::fmt::Display) {}
1873-
```
1874-
1875-
The code is trying to specify that we want to receive a signed 32-bit integer
1876-
which also implements `Display`. This doesn't make sense: when we pass `i32`, a
1877-
concrete type, it implicitly includes all of the traits that it implements.
1878-
This includes `Display`, `Debug`, `Clone`, and a host of others.
1879-
1880-
If `i32` implements the trait we desire, there's no need to specify the trait
1881-
separately. If it does not, then we need to `impl` the trait for `i32` before
1882-
passing it into `foo`. Either way, a fixed definition for `foo` will look like
1883-
the following:
1884-
1885-
```
1886-
fn foo(bar: i32) {}
1887-
```
1888-
1889-
To learn more about traits, take a look at the Book:
1890-
1891-
https://doc.rust-lang.org/book/traits.html
1892-
"##,
1893-
1894-
E0178: r##"
1895-
In types, the `+` type operator has low precedence, so it is often necessary
1896-
to use parentheses.
1897-
1898-
For example:
1899-
1900-
```compile_fail,E0178
1901-
trait Foo {}
1902-
1903-
struct Bar<'a> {
1904-
w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
1905-
x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
1906-
y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
1907-
z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
1908-
}
1909-
```
1910-
1911-
More details can be found in [RFC 438].
1912-
1913-
[RFC 438]: https://github.com/rust-lang/rfcs/pull/438
1914-
"##,
1915-
19161867
E0182: r##"
19171868
You bound an associated type in an expression path which is not
19181869
allowed.
@@ -4152,6 +4103,7 @@ register_diagnostics! {
41524103
// E0163, // merged into E0071
41534104
// E0167,
41544105
// E0168,
4106+
// E0172, // non-trait found in a type sum, moved to resolve
41554107
// E0173, // manual implementations of unboxed closure traits are experimental
41564108
// E0174,
41574109
E0183,

0 commit comments

Comments
 (0)