Skip to content

Commit 0947f40

Browse files
committed
Move unsafety out of the subtyping relation and into coercion.
1 parent 1b0f0ad commit 0947f40

File tree

19 files changed

+173
-59
lines changed

19 files changed

+173
-59
lines changed

src/librustc/middle/astencode.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,14 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
993993
})
994994
}
995995

996+
ty::AdjustUnsafeFnPointer => {
997+
this.emit_enum_variant("AdjustUnsafeFnPointer", 2, 0, |_| {
998+
Ok(())
999+
})
1000+
}
1001+
9961002
ty::AdjustDerefRef(ref auto_deref_ref) => {
997-
this.emit_enum_variant("AdjustDerefRef", 2, 2, |this| {
1003+
this.emit_enum_variant("AdjustDerefRef", 3, 2, |this| {
9981004
this.emit_enum_variant_arg(0,
9991005
|this| Ok(this.emit_auto_deref_ref(ecx, auto_deref_ref)))
10001006
})
@@ -1619,6 +1625,9 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
16191625
ty::AdjustReifyFnPointer(def_id)
16201626
}
16211627
2 => {
1628+
ty::AdjustUnsafeFnPointer
1629+
}
1630+
3 => {
16221631
let auto_deref_ref: ty::AutoDerefRef =
16231632
this.read_enum_variant_arg(0,
16241633
|this| Ok(this.read_auto_deref_ref(dcx))).unwrap();

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
790790
None => { }
791791
Some(adjustment) => {
792792
match *adjustment {
793-
ty::AdjustReifyFnPointer(..) => {
793+
ty::AdjustReifyFnPointer(..) |
794+
ty::AdjustUnsafeFnPointer(..) => {
794795
// Creating a closure/fn-pointer consumes the
795796
// input and stores it into the resulting
796797
// rvalue.

src/librustc/middle/infer/bivariate.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ use middle::infer::{cres};
3333
use middle::infer::type_variable::{BiTo};
3434
use util::ppaux::{Repr};
3535

36-
use syntax::ast::{Unsafety};
37-
3836
pub struct Bivariate<'f, 'tcx: 'f> {
3937
fields: CombineFields<'f, 'tcx>
4038
}
@@ -74,14 +72,6 @@ impl<'f, 'tcx> Combine<'tcx> for Bivariate<'f, 'tcx> {
7472
Ok(a)
7573
}
7674

77-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
78-
if a != b {
79-
Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
80-
} else {
81-
Ok(a)
82-
}
83-
}
84-
8575
fn builtin_bounds(&self,
8676
a: BuiltinBounds,
8777
b: BuiltinBounds)

src/librustc/middle/infer/combine.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,13 @@ pub trait Combine<'tcx> : Sized {
263263
self.tys_with_variance(ty::Contravariant, a, b).and_then(|t| Ok(t))
264264
}
265265

266-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety>;
266+
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
267+
if a != b {
268+
Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
269+
} else {
270+
Ok(a)
271+
}
272+
}
267273

268274
fn abi(&self, a: abi::Abi, b: abi::Abi) -> cres<'tcx, abi::Abi> {
269275
if a == b {

src/librustc/middle/infer/equate.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use middle::infer::{Subtype};
1616
use middle::infer::type_variable::{EqTo};
1717
use util::ppaux::{Repr};
1818

19-
use syntax::ast::Unsafety;
20-
2119
pub struct Equate<'f, 'tcx: 'f> {
2220
fields: CombineFields<'f, 'tcx>
2321
}
@@ -54,15 +52,6 @@ impl<'f, 'tcx> Combine<'tcx> for Equate<'f, 'tcx> {
5452
Ok(a)
5553
}
5654

57-
58-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
59-
if a != b {
60-
Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
61-
} else {
62-
Ok(a)
63-
}
64-
}
65-
6655
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
6756
debug!("{}.tys({}, {})", self.tag(),
6857
a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));

src/librustc/middle/infer/glb.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use super::{cres};
1515
use super::Subtype;
1616

1717
use middle::ty::{self, Ty};
18-
use syntax::ast::{MutImmutable, MutMutable, Unsafety};
19-
use util::ppaux::mt_to_string;
2018
use util::ppaux::Repr;
2119

2220
/// "Greatest lower bound" (common subtype)
@@ -55,14 +53,6 @@ impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
5553
}
5654
}
5755

58-
59-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
60-
match (a, b) {
61-
(Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
62-
(Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
63-
}
64-
}
65-
6656
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
6757
debug!("{}.regions({}, {})",
6858
self.tag(),

src/librustc/middle/infer/lub.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use super::{cres};
1515
use super::{Subtype};
1616

1717
use middle::ty::{self, Ty};
18-
use syntax::ast::{MutMutable, MutImmutable, Unsafety};
19-
use util::ppaux::mt_to_string;
2018
use util::ppaux::Repr;
2119

2220
/// "Least upper bound" (common supertype)
@@ -55,13 +53,6 @@ impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
5553
}
5654
}
5755

58-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
59-
match (a, b) {
60-
(Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),
61-
(Unsafety::Normal, Unsafety::Normal) => Ok(Unsafety::Normal),
62-
}
63-
}
64-
6556
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
6657
debug!("{}.regions({}, {})",
6758
self.tag(),

src/librustc/middle/infer/sub.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use super::combine::*;
12-
use super::{cres, CresCompare};
12+
use super::{cres};
1313
use super::higher_ranked::HigherRankedRelations;
1414
use super::{Subtype};
1515
use super::type_variable::{SubtypeOf, SupertypeOf};
@@ -18,9 +18,6 @@ use middle::ty::{self, Ty};
1818
use middle::ty::TyVar;
1919
use util::ppaux::{Repr};
2020

21-
use syntax::ast::{MutImmutable, MutMutable, Unsafety};
22-
23-
2421
/// "Greatest lower bound" (common subtype)
2522
pub struct Sub<'f, 'tcx: 'f> {
2623
fields: CombineFields<'f, 'tcx>
@@ -66,12 +63,6 @@ impl<'f, 'tcx> Combine<'tcx> for Sub<'f, 'tcx> {
6663
Ok(a)
6764
}
6865

69-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
70-
self.lub().unsafeties(a, b).compare(b, || {
71-
ty::terr_unsafety_mismatch(expected_found(self, a, b))
72-
})
73-
}
74-
7566
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
7667
debug!("{}.tys({}, {})", self.tag(),
7768
a.repr(self.tcx()), b.repr(self.tcx()));

src/librustc/middle/mem_categorization.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
428428

429429
Some(adjustment) => {
430430
match *adjustment {
431-
ty::AdjustReifyFnPointer(..) => {
431+
ty::AdjustReifyFnPointer(..) |
432+
ty::AdjustUnsafeFnPointer(..) => {
432433
debug!("cat_expr(AdjustReifyFnPointer): {}",
433434
expr.repr(self.tcx()));
434435
// Convert a bare fn to a closure by adding NULL env.

src/librustc/middle/ty.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ pub enum Variance {
282282
#[derive(Clone, Debug)]
283283
pub enum AutoAdjustment<'tcx> {
284284
AdjustReifyFnPointer(ast::DefId), // go from a fn-item type to a fn-pointer type
285+
AdjustUnsafeFnPointer, // go from a safe fn pointer to an unsafe fn pointer
285286
AdjustDerefRef(AutoDerefRef<'tcx>)
286287
}
287288

@@ -2637,6 +2638,17 @@ impl<'tcx> ctxt<'tcx> {
26372638
substs
26382639
}
26392640

2641+
/// Create an unsafe fn ty based on a safe fn ty.
2642+
pub fn safe_to_unsafe_fn_ty(&self, bare_fn: &BareFnTy<'tcx>) -> Ty<'tcx> {
2643+
assert_eq!(bare_fn.unsafety, ast::Unsafety::Normal);
2644+
let unsafe_fn_ty_a = self.mk_bare_fn(ty::BareFnTy {
2645+
unsafety: ast::Unsafety::Unsafe,
2646+
abi: bare_fn.abi,
2647+
sig: bare_fn.sig.clone()
2648+
});
2649+
ty::mk_bare_fn(self, None, unsafe_fn_ty_a)
2650+
}
2651+
26402652
pub fn mk_bare_fn(&self, bare_fn: BareFnTy<'tcx>) -> &'tcx BareFnTy<'tcx> {
26412653
if let Some(bare_fn) = self.bare_fn_interner.borrow().get(&bare_fn) {
26422654
return *bare_fn;
@@ -4526,6 +4538,18 @@ pub fn adjust_ty<'tcx, F>(cx: &ctxt<'tcx>,
45264538
}
45274539
}
45284540

4541+
AdjustUnsafeFnPointer => {
4542+
match unadjusted_ty.sty {
4543+
ty::ty_bare_fn(None, b) => cx.safe_to_unsafe_fn_ty(b),
4544+
ref b => {
4545+
cx.sess.bug(
4546+
&format!("AdjustReifyFnPointer adjustment on non-fn-item: \
4547+
{:?}",
4548+
b));
4549+
}
4550+
}
4551+
}
4552+
45294553
AdjustDerefRef(ref adj) => {
45304554
let mut adjusted_ty = unadjusted_ty;
45314555

@@ -6695,6 +6719,7 @@ impl<'tcx> AutoAdjustment<'tcx> {
66956719
pub fn is_identity(&self) -> bool {
66966720
match *self {
66976721
AdjustReifyFnPointer(..) => false,
6722+
AdjustUnsafeFnPointer(..) => false,
66986723
AdjustDerefRef(ref r) => r.is_identity(),
66996724
}
67006725
}
@@ -6844,6 +6869,9 @@ impl<'tcx> Repr<'tcx> for AutoAdjustment<'tcx> {
68446869
AdjustReifyFnPointer(def_id) => {
68456870
format!("AdjustReifyFnPointer({})", def_id.repr(tcx))
68466871
}
6872+
AdjustUnsafeFnPointer => {
6873+
format!("AdjustUnsafeFnPointer")
6874+
}
68476875
AdjustDerefRef(ref data) => {
68486876
data.repr(tcx)
68496877
}

0 commit comments

Comments
 (0)