Skip to content

Commit 2efe865

Browse files
committed
AST/HIR: Merge ObjectSum and PolyTraitRef
1 parent 8284046 commit 2efe865

File tree

22 files changed

+119
-240
lines changed

22 files changed

+119
-240
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 1 addition & 5 deletions
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+
TyObjectSum(ref bounds) => {
574570
walk_list!(visitor, visit_ty_param_bound, bounds);
575571
}
576572
TyImplTrait(ref bounds) => {

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 5 deletions
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::ObjectSum(ref bounds) => {
321+
hir::TyObjectSum(self.lower_bounds(bounds))
325322
}
326323
TyKind::ImplTrait(ref bounds) => {
327324
hir::TyImplTrait(self.lower_bounds(bounds))

src/librustc/hir/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
TyObjectSum(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

Lines changed: 1 addition & 5 deletions
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::TyObjectSum(ref bounds) => {
426422
self.print_bounds("", &bounds[..])?;
427423
}
428424
hir::TyImplTrait(ref bounds) => {

src/librustc_incremental/calculate_svh/svh_visitor.rs

Lines changed: 0 additions & 2 deletions
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
@@ -458,7 +457,6 @@ fn saw_ty(node: &Ty_) -> SawTyComponent {
458457
TyTup(..) => SawTyTup,
459458
TyPath(_) => SawTyPath,
460459
TyObjectSum(..) => SawTyObjectSum,
461-
TyPolyTraitRef(..) => SawTyPolyTraitRef,
462460
TyImplTrait(..) => SawTyImplTrait,
463461
TyTypeof(..) => SawTyTypeof,
464462
TyInfer => SawTyInfer

src/librustc_passes/ast_validation.rs

Lines changed: 1 addition & 2 deletions
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::ObjectSum(ref bounds) => {
148147
self.no_questions_in_bounds(bounds, "trait object types", false);
149148
}
150149
_ => {}

src/librustc_typeck/astconv.rs

Lines changed: 2 additions & 86 deletions
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>)
@@ -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::TyObjectSum(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

Lines changed: 1 addition & 49 deletions
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,

src/librustdoc/clean/mod.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,9 +1504,6 @@ pub enum Type {
15041504
// _
15051505
Infer,
15061506

1507-
// for<'a> Foo(&'a)
1508-
PolyTraitRef(Vec<TyParamBound>),
1509-
15101507
// impl TraitA+TraitB
15111508
ImplTrait(Vec<TyParamBound>),
15121509
}
@@ -1768,24 +1765,26 @@ impl Clean<Type> for hir::Ty {
17681765
trait_: box resolve_type(cx, trait_path.clean(cx), self.id)
17691766
}
17701767
}
1771-
TyObjectSum(ref lhs, ref bounds) => {
1772-
let lhs_ty = lhs.clean(cx);
1768+
TyObjectSum(ref bounds) => {
1769+
let lhs_ty = bounds[0].clean(cx);
17731770
match lhs_ty {
1774-
ResolvedPath { path, typarams: None, did, is_generic } => {
1775-
ResolvedPath {
1776-
path: path,
1777-
typarams: Some(bounds.clean(cx)),
1778-
did: did,
1779-
is_generic: is_generic,
1771+
TraitBound(poly_trait, ..) => {
1772+
match poly_trait.trait_ {
1773+
ResolvedPath { path, typarams: None, did, is_generic } => {
1774+
ResolvedPath {
1775+
path: path,
1776+
typarams: Some(bounds[1..].clean(cx)),
1777+
did: did,
1778+
is_generic: is_generic,
1779+
}
1780+
}
1781+
_ => Infer // shouldn't happen
17801782
}
17811783
}
1782-
_ => {
1783-
lhs_ty // shouldn't happen
1784-
}
1784+
_ => Infer // shouldn't happen
17851785
}
17861786
}
17871787
TyBareFn(ref barefn) => BareFunction(box barefn.clean(cx)),
1788-
TyPolyTraitRef(ref bounds) => PolyTraitRef(bounds.clean(cx)),
17891788
TyImplTrait(ref bounds) => ImplTrait(bounds.clean(cx)),
17901789
TyInfer => Infer,
17911790
TyTypeof(..) => panic!("Unimplemented type {:?}", self.node),

src/librustdoc/html/format.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -679,19 +679,6 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
679679
}
680680
}
681681
}
682-
clean::PolyTraitRef(ref bounds) => {
683-
for (i, bound) in bounds.iter().enumerate() {
684-
if i != 0 {
685-
write!(f, " + ")?;
686-
}
687-
if f.alternate() {
688-
write!(f, "{:#}", *bound)?;
689-
} else {
690-
write!(f, "{}", *bound)?;
691-
}
692-
}
693-
Ok(())
694-
}
695682
clean::ImplTrait(ref bounds) => {
696683
write!(f, "impl ")?;
697684
for (i, bound) in bounds.iter().enumerate() {

src/libsyntax/ast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,11 +1357,11 @@ pub enum TyKind {
13571357
///
13581358
/// Type parameters are stored in the Path itself
13591359
Path(Option<QSelf>, Path),
1360-
/// Something like `A+B`. Note that `B` must always be a path.
1361-
ObjectSum(P<Ty>, TyParamBounds),
1362-
/// A type like `for<'a> Foo<&'a Bar>`
1363-
PolyTraitRef(TyParamBounds),
1364-
/// An `impl TraitA+TraitB` type.
1360+
/// A trait object type `Bound1 + Bound2 + Bound3`
1361+
/// where `Bound` is a trait or a lifetime.
1362+
ObjectSum(TyParamBounds),
1363+
/// An `impl Bound1 + Bound2 + Bound3` type
1364+
/// where `Bound` is a trait or a lifetime.
13651365
ImplTrait(TyParamBounds),
13661366
/// No-op; kept solely so that we can pretty-print faithfully
13671367
Paren(P<Ty>),

src/libsyntax/diagnostic_list.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@
1515
// In vim you can `:set tw=80` and use `gq` to wrap paragraphs. Use `:set tw=0` to disable.
1616
register_long_diagnostics! {
1717

18+
E0178: r##"
19+
In types, the `+` type operator has low precedence, so it is often necessary
20+
to use parentheses.
21+
22+
For example:
23+
24+
```compile_fail,E0178
25+
trait Foo {}
26+
27+
struct Bar<'a> {
28+
w: &'a Foo + Copy, // error, use &'a (Foo + Copy)
29+
x: &'a Foo + 'a, // error, use &'a (Foo + 'a)
30+
y: &'a mut Foo + 'a, // error, use &'a mut (Foo + 'a)
31+
z: fn() -> Foo + 'a, // error, use fn() -> (Foo + 'a)
32+
}
33+
```
34+
35+
More details can be found in [RFC 438].
36+
37+
[RFC 438]: https://github.com/rust-lang/rfcs/pull/438
38+
"##,
39+
1840
E0534: r##"
1941
The `inline` attribute was malformed.
2042

0 commit comments

Comments
 (0)