Skip to content

Commit 0947f40

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

19 files changed

+173
-59
lines changed

Diff for: src/librustc/middle/astencode.rs

+10-1
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();

Diff for: src/librustc/middle/expr_use_visitor.rs

+2-1
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.

Diff for: src/librustc/middle/infer/bivariate.rs

-10
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)

Diff for: src/librustc/middle/infer/combine.rs

+7-1
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 {

Diff for: src/librustc/middle/infer/equate.rs

-11
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));

Diff for: src/librustc/middle/infer/glb.rs

-10
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(),

Diff for: src/librustc/middle/infer/lub.rs

-9
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(),

Diff for: src/librustc/middle/infer/sub.rs

+1-10
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()));

Diff for: src/librustc/middle/mem_categorization.rs

+2-1
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.

Diff for: src/librustc/middle/ty.rs

+28
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
}

Diff for: src/librustc_trans/trans/consts.rs

+3
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
253253
// FIXME(#19925) once fn item types are
254254
// zero-sized, we'll need to do something here
255255
}
256+
Some(ty::AdjustUnsafeFnPointer) => {
257+
// purely a type-level thing
258+
}
256259
Some(ty::AdjustDerefRef(adj)) => {
257260
let mut ty = ety;
258261
// Save the last autoderef in case we can avoid it.

Diff for: src/librustc_trans/trans/expr.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use trans::monomorphize;
7272
use trans::tvec;
7373
use trans::type_of;
7474
use middle::ty::{struct_fields, tup_fields};
75-
use middle::ty::{AdjustDerefRef, AdjustReifyFnPointer, AutoUnsafe};
75+
use middle::ty::{AdjustDerefRef, AdjustReifyFnPointer, AdjustUnsafeFnPointer, AutoUnsafe};
7676
use middle::ty::{AutoPtr};
7777
use middle::ty::{self, Ty};
7878
use middle::ty::MethodCall;
@@ -396,6 +396,9 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
396396
// FIXME(#19925) once fn item types are
397397
// zero-sized, we'll need to do something here
398398
}
399+
AdjustUnsafeFnPointer => {
400+
// purely a type-level thing
401+
}
399402
AdjustDerefRef(ref adj) => {
400403
let (autoderefs, use_autoref) = match adj.autoref {
401404
// Extracting a value from a box counts as a deref, but if we are

Diff for: src/librustc_typeck/check/coercion.rs

+40
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
143143
// require double indirection).
144144
self.coerce_from_fn_item(a, a_def_id, a_f, b)
145145
}
146+
ty::ty_bare_fn(None, a_f) => {
147+
// We permit coercion of fn pointers to drop the
148+
// unsafe qualifier.
149+
self.coerce_from_fn_pointer(a, a_f, b)
150+
}
146151
_ => {
147152
// Otherwise, just use subtyping rules.
148153
self.subtype(a, b)
@@ -411,6 +416,41 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
411416
)
412417
}
413418

419+
fn coerce_from_fn_pointer(&self,
420+
a: Ty<'tcx>,
421+
fn_ty_a: &'tcx ty::BareFnTy<'tcx>,
422+
b: Ty<'tcx>)
423+
-> CoerceResult<'tcx>
424+
{
425+
/*!
426+
* Attempts to coerce from the type of a Rust function item
427+
* into a closure or a `proc`.
428+
*/
429+
430+
self.unpack_actual_value(b, |b| {
431+
debug!("coerce_from_fn_pointer(a={}, b={})",
432+
a.repr(self.tcx()), b.repr(self.tcx()));
433+
434+
match b.sty {
435+
ty::ty_bare_fn(None, fn_ty_b) => {
436+
match (fn_ty_a.unsafety, fn_ty_b.unsafety) {
437+
(ast::Unsafety::Normal, ast::Unsafety::Unsafe) => {
438+
let unsafe_a = self.tcx().safe_to_unsafe_fn_ty(fn_ty_a);
439+
try!(self.subtype(unsafe_a, b));
440+
Ok(Some(ty::AdjustUnsafeFnPointer))
441+
}
442+
_ => {
443+
self.subtype(a, b)
444+
}
445+
}
446+
}
447+
_ => {
448+
return self.subtype(a, b)
449+
}
450+
}
451+
})
452+
}
453+
414454
fn coerce_from_fn_item(&self,
415455
a: Ty<'tcx>,
416456
fn_def_id_a: ast::DefId,

Diff for: src/librustc_typeck/check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15321532
span: Span,
15331533
adj: &ty::AutoAdjustment<'tcx>) {
15341534
match *adj {
1535-
ty::AdjustReifyFnPointer(..) => {
1536-
}
1535+
ty::AdjustReifyFnPointer(..) => { }
1536+
ty::AdjustUnsafeFnPointer => { }
15371537
ty::AdjustDerefRef(ref d_r) => {
15381538
match d_r.autoref {
15391539
Some(ref a_r) => {

Diff for: src/librustc_typeck/check/writeback.rs

+4
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
265265
ty::AdjustReifyFnPointer(def_id)
266266
}
267267

268+
ty::AdjustUnsafeFnPointer => {
269+
ty::AdjustUnsafeFnPointer
270+
}
271+
268272
ty::AdjustDerefRef(adj) => {
269273
for autoderef in 0..adj.autoderefs {
270274
let method_call = MethodCall::autoderef(id, autoderef);

Diff for: src/test/compile-fail/unsafe-subtyping.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Check that safe fns are not a subtype of unsafe fns.
12+
13+
fn foo(x: Option<fn(i32)>) -> Option<unsafe fn(i32)> {
14+
x //~ ERROR mismatched types
15+
}
16+
17+
fn bar(x: fn(i32)) -> unsafe fn(i32) {
18+
x // OK, coercion!
19+
}
20+
21+
fn main() { }

0 commit comments

Comments
 (0)