Skip to content

Commit 49de82c

Browse files
committed
Issue #5656: Make &self not mean "&'self self"
Fixes #5656. Fixes #5541.
1 parent 3322595 commit 49de82c

File tree

16 files changed

+336
-295
lines changed

16 files changed

+336
-295
lines changed

src/librustc/middle/kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use middle::liveness;
1616
use middle::pat_util;
1717
use middle::ty;
1818
use middle::typeck;
19-
use util::ppaux::{Repr, ty_to_str, tys_to_str};
19+
use util::ppaux::{Repr, ty_to_str};
2020

2121
use syntax::ast::*;
2222
use syntax::attr::attrs_contains_name;

src/librustc/middle/region.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,6 @@ pub struct DetermineRpCtxt {
544544
// see long discussion on region_is_relevant().
545545
anon_implies_rp: bool,
546546

547-
// true when we are not within an &self method.
548-
// see long discussion on region_is_relevant().
549-
self_implies_rp: bool,
550-
551547
// encodes the context of the current type; invariant if
552548
// mutable, covariant otherwise
553549
ambient_variance: region_variance,
@@ -689,7 +685,7 @@ pub impl DetermineRpCtxt {
689685
false
690686
}
691687
Some(ref l) if l.ident == special_idents::self_ => {
692-
self.self_implies_rp
688+
true
693689
}
694690
Some(_) => {
695691
false
@@ -700,23 +696,18 @@ pub impl DetermineRpCtxt {
700696
fn with(@mut self,
701697
item_id: ast::node_id,
702698
anon_implies_rp: bool,
703-
self_implies_rp: bool,
704699
f: &fn()) {
705700
let old_item_id = self.item_id;
706701
let old_anon_implies_rp = self.anon_implies_rp;
707-
let old_self_implies_rp = self.self_implies_rp;
708702
self.item_id = item_id;
709703
self.anon_implies_rp = anon_implies_rp;
710-
self.self_implies_rp = self_implies_rp;
711-
debug!("with_item_id(%d, %b, %b)",
704+
debug!("with_item_id(%d, %b)",
712705
item_id,
713-
anon_implies_rp,
714-
self_implies_rp);
706+
anon_implies_rp);
715707
let _i = ::util::common::indenter();
716708
f();
717709
self.item_id = old_item_id;
718710
self.anon_implies_rp = old_anon_implies_rp;
719-
self.self_implies_rp = old_self_implies_rp;
720711
}
721712

722713
fn with_ambient_variance(@mut self, variance: region_variance, f: &fn()) {
@@ -730,7 +721,7 @@ pub impl DetermineRpCtxt {
730721
pub fn determine_rp_in_item(item: @ast::item,
731722
&&cx: @mut DetermineRpCtxt,
732723
visitor: visit::vt<@mut DetermineRpCtxt>) {
733-
do cx.with(item.id, true, true) {
724+
do cx.with(item.id, true) {
734725
visit::visit_item(item, cx, visitor);
735726
}
736727
}
@@ -742,12 +733,7 @@ pub fn determine_rp_in_fn(fk: &visit::fn_kind,
742733
_: ast::node_id,
743734
&&cx: @mut DetermineRpCtxt,
744735
visitor: visit::vt<@mut DetermineRpCtxt>) {
745-
let self_implies_rp = match fk {
746-
&visit::fk_method(_, _, m) => !m.self_ty.node.is_borrowed(),
747-
_ => true
748-
};
749-
750-
do cx.with(cx.item_id, false, self_implies_rp) {
736+
do cx.with(cx.item_id, false) {
751737
do cx.with_ambient_variance(rv_contravariant) {
752738
for decl.inputs.each |a| {
753739
(visitor.visit_ty)(a.ty, cx, visitor);
@@ -763,7 +749,7 @@ pub fn determine_rp_in_fn(fk: &visit::fn_kind,
763749
pub fn determine_rp_in_ty_method(ty_m: &ast::ty_method,
764750
&&cx: @mut DetermineRpCtxt,
765751
visitor: visit::vt<@mut DetermineRpCtxt>) {
766-
do cx.with(cx.item_id, false, !ty_m.self_ty.node.is_borrowed()) {
752+
do cx.with(cx.item_id, false) {
767753
visit::visit_ty_method(ty_m, cx, visitor);
768754
}
769755
}
@@ -868,7 +854,7 @@ pub fn determine_rp_in_ty(ty: @ast::Ty,
868854
ast::ty_bare_fn(@ast::TyBareFn {decl: ref decl, _}) => {
869855
// fn() binds the & region, so do not consider &T types that
870856
// appear *inside* a fn() type to affect the enclosing item:
871-
do cx.with(cx.item_id, false, true) {
857+
do cx.with(cx.item_id, false) {
872858
// parameters are contravariant
873859
do cx.with_ambient_variance(rv_contravariant) {
874860
for decl.inputs.each |a| {
@@ -929,7 +915,6 @@ pub fn determine_rp_in_crate(sess: Session,
929915
worklist: ~[],
930916
item_id: 0,
931917
anon_implies_rp: false,
932-
self_implies_rp: true,
933918
ambient_variance: rv_covariant
934919
};
935920

src/librustc/middle/subst.rs

+30-17
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,7 @@ impl EffectfulSubst for ty::t {
6262
_ => {
6363
ty::fold_regions_and_ty(
6464
tcx, *self,
65-
|r| match r {
66-
ty::re_bound(ty::br_self) => {
67-
match substs.self_r {
68-
None => {
69-
tcx.sess.bug(
70-
fmt!("ty::subst: \
71-
Reference to self region when \
72-
given substs with no self region, \
73-
ty = %s",
74-
self.repr(tcx)));
75-
}
76-
Some(self_r) => self_r
77-
}
78-
}
79-
_ => r
80-
},
65+
|r| r.subst(tcx, substs),
8166
|t| t.effectfulSubst(tcx, substs),
8267
|t| t.effectfulSubst(tcx, substs))
8368
}
@@ -118,7 +103,7 @@ impl Subst for ty::TraitRef {
118103
impl Subst for ty::substs {
119104
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::substs {
120105
ty::substs {
121-
self_r: self.self_r,
106+
self_r: self.self_r.subst(tcx, substs),
122107
self_ty: self.self_ty.map(|typ| typ.subst(tcx, substs)),
123108
tps: self.tps.map(|typ| typ.subst(tcx, substs))
124109
}
@@ -166,6 +151,34 @@ impl Subst for ty::Generics {
166151
}
167152
}
168153

154+
impl Subst for ty::Region {
155+
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::Region {
156+
// Note: This routine only handles the self region, because it
157+
// is only concerned with substitutions of regions that appear
158+
// in types. Region substitution of the bound regions that
159+
// appear in a function signature is done using the
160+
// specialized routine
161+
// `middle::typeck::check::regionmanip::replace_bound_regions_in_fn_sig()`.
162+
// As we transition to the new region syntax this distinction
163+
// will most likely disappear.
164+
match self {
165+
&ty::re_bound(ty::br_self) => {
166+
match substs.self_r {
167+
None => {
168+
tcx.sess.bug(
169+
fmt!("ty::Region#subst(): \
170+
Reference to self region when \
171+
given substs with no self region: %s",
172+
substs.repr(tcx)));
173+
}
174+
Some(self_r) => self_r
175+
}
176+
}
177+
_ => *self
178+
}
179+
}
180+
}
181+
169182
impl Subst for ty::ty_param_bounds_and_ty {
170183
fn subst(&self, tcx: ty::ctxt, substs: &ty::substs) -> ty::ty_param_bounds_and_ty {
171184
ty::ty_param_bounds_and_ty {

src/librustc/middle/ty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ use middle::subst::Subst;
2424
use middle::typeck;
2525
use middle;
2626
use util::ppaux::{note_and_explain_region, bound_region_to_str};
27-
use util::ppaux::{region_to_str, vstore_to_str};
28-
use util::ppaux::{trait_store_to_str, ty_to_str, tys_to_str};
27+
use util::ppaux::{trait_store_to_str, ty_to_str, vstore_to_str};
2928
use util::ppaux::Repr;
3029
use util::common::{indenter};
3130

0 commit comments

Comments
 (0)