Skip to content

Commit c109189

Browse files
committed
Auto merge of #23452 - nikomatsakis:unsafety-subtyping, r=nrc
Safe fns are no longer subtypes of unsafe fns, but you can coerce from one to the other. This is a [breaking-change] in that impl fns must now be declared `unsafe` if the trait is declared `unsafe`. In some rare cases, the subtyping change may also direct affect you, but no such cases were encountered in practice. Fixes #23449. r? @nrc
2 parents 1ae32de + 0947f40 commit c109189

19 files changed

+191
-159
lines changed

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

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.

src/librustc/middle/infer/bivariate.rs

-20
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,24 +72,6 @@ impl<'f, 'tcx> Combine<'tcx> for Bivariate<'f, 'tcx> {
7472
Ok(a)
7573
}
7674

77-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
78-
debug!("mts({} <: {})",
79-
a.repr(self.fields.infcx.tcx),
80-
b.repr(self.fields.infcx.tcx));
81-
82-
if a.mutbl != b.mutbl { return Err(ty::terr_mutability); }
83-
let t = try!(self.tys(a.ty, b.ty));
84-
Ok(ty::mt { mutbl: a.mutbl, ty: t })
85-
}
86-
87-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
88-
if a != b {
89-
Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
90-
} else {
91-
Ok(a)
92-
}
93-
}
94-
9575
fn builtin_bounds(&self,
9676
a: BuiltinBounds,
9777
b: BuiltinBounds)

src/librustc/middle/infer/combine.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,24 @@ pub trait Combine<'tcx> : Sized {
7474
fn lub<'a>(&'a self) -> Lub<'a, 'tcx> { Lub(self.fields().clone()) }
7575
fn glb<'a>(&'a self) -> Glb<'a, 'tcx> { Glb(self.fields().clone()) }
7676

77-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>>;
77+
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
78+
debug!("{}.mts({}, {})",
79+
self.tag(),
80+
a.repr(self.tcx()),
81+
b.repr(self.tcx()));
82+
83+
if a.mutbl != b.mutbl {
84+
Err(ty::terr_mutability)
85+
} else {
86+
let mutbl = a.mutbl;
87+
let variance = match mutbl {
88+
ast::MutImmutable => ty::Covariant,
89+
ast::MutMutable => ty::Invariant,
90+
};
91+
let ty = try!(self.tys_with_variance(variance, a.ty, b.ty));
92+
Ok(ty::mt {ty: ty, mutbl: mutbl})
93+
}
94+
}
7895

7996
fn tys_with_variance(&self, variance: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
8097
-> cres<'tcx, Ty<'tcx>>;
@@ -246,7 +263,13 @@ pub trait Combine<'tcx> : Sized {
246263
self.tys_with_variance(ty::Contravariant, a, b).and_then(|t| Ok(t))
247264
}
248265

249-
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+
}
250273

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

src/librustc/middle/infer/equate.rs

-20
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,24 +52,6 @@ impl<'f, 'tcx> Combine<'tcx> for Equate<'f, 'tcx> {
5452
Ok(a)
5553
}
5654

57-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
58-
debug!("mts({} <: {})",
59-
a.repr(self.fields.infcx.tcx),
60-
b.repr(self.fields.infcx.tcx));
61-
62-
if a.mutbl != b.mutbl { return Err(ty::terr_mutability); }
63-
let t = try!(self.tys(a.ty, b.ty));
64-
Ok(ty::mt { mutbl: a.mutbl, ty: t })
65-
}
66-
67-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
68-
if a != b {
69-
Err(ty::terr_unsafety_mismatch(expected_found(self, a, b)))
70-
} else {
71-
Ok(a)
72-
}
73-
}
74-
7555
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
7656
debug!("{}.tys({}, {})", self.tag(),
7757
a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));

src/librustc/middle/infer/glb.rs

-40
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,44 +53,6 @@ impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
5553
}
5654
}
5755

58-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
59-
let tcx = self.fields.infcx.tcx;
60-
61-
debug!("{}.mts({}, {})",
62-
self.tag(),
63-
mt_to_string(tcx, a),
64-
mt_to_string(tcx, b));
65-
66-
match (a.mutbl, b.mutbl) {
67-
// If one side or both is mut, then the GLB must use
68-
// the precise type from the mut side.
69-
(MutMutable, MutMutable) => {
70-
let t = try!(self.equate().tys(a.ty, b.ty));
71-
Ok(ty::mt {ty: t, mutbl: MutMutable})
72-
}
73-
74-
// If one side or both is immutable, we can use the GLB of
75-
// both sides but mutbl must be `MutImmutable`.
76-
(MutImmutable, MutImmutable) => {
77-
let t = try!(self.tys(a.ty, b.ty));
78-
Ok(ty::mt {ty: t, mutbl: MutImmutable})
79-
}
80-
81-
// There is no mutual subtype of these combinations.
82-
(MutMutable, MutImmutable) |
83-
(MutImmutable, MutMutable) => {
84-
Err(ty::terr_mutability)
85-
}
86-
}
87-
}
88-
89-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
90-
match (a, b) {
91-
(Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
92-
(Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
93-
}
94-
}
95-
9656
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
9757
debug!("{}.regions({}, {})",
9858
self.tag(),

src/librustc/middle/infer/lub.rs

-35
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,39 +53,6 @@ impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
5553
}
5654
}
5755

58-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
59-
let tcx = self.tcx();
60-
61-
debug!("{}.mts({}, {})",
62-
self.tag(),
63-
mt_to_string(tcx, a),
64-
mt_to_string(tcx, b));
65-
66-
if a.mutbl != b.mutbl {
67-
return Err(ty::terr_mutability)
68-
}
69-
70-
let m = a.mutbl;
71-
match m {
72-
MutImmutable => {
73-
let t = try!(self.tys(a.ty, b.ty));
74-
Ok(ty::mt {ty: t, mutbl: m})
75-
}
76-
77-
MutMutable => {
78-
let t = try!(self.equate().tys(a.ty, b.ty));
79-
Ok(ty::mt {ty: t, mutbl: m})
80-
}
81-
}
82-
}
83-
84-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
85-
match (a, b) {
86-
(Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),
87-
(Unsafety::Normal, Unsafety::Normal) => Ok(Unsafety::Normal),
88-
}
89-
}
90-
9156
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
9257
debug!("{}.regions({}, {})",
9358
self.tag(),

src/librustc/middle/infer/sub.rs

+1-34
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,36 +63,6 @@ impl<'f, 'tcx> Combine<'tcx> for Sub<'f, 'tcx> {
6663
Ok(a)
6764
}
6865

69-
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
70-
debug!("mts({} <: {})",
71-
a.repr(self.tcx()),
72-
b.repr(self.tcx()));
73-
74-
if a.mutbl != b.mutbl {
75-
return Err(ty::terr_mutability);
76-
}
77-
78-
match b.mutbl {
79-
MutMutable => {
80-
// If supertype is mut, subtype must match exactly
81-
// (i.e., invariant if mut):
82-
try!(self.equate().tys(a.ty, b.ty));
83-
}
84-
MutImmutable => {
85-
// Otherwise we can be covariant:
86-
try!(self.tys(a.ty, b.ty));
87-
}
88-
}
89-
90-
Ok(*a) // return is meaningless in sub, just return *a
91-
}
92-
93-
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
94-
self.lub().unsafeties(a, b).compare(b, || {
95-
ty::terr_unsafety_mismatch(expected_found(self, a, b))
96-
})
97-
}
98-
9966
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
10067
debug!("{}.tys({}, {})", self.tag(),
10168
a.repr(self.tcx()), b.repr(self.tcx()));

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.

src/librustc/middle/ty.rs

+28
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ pub enum Variance {
281281
#[derive(Clone, Debug)]
282282
pub enum AutoAdjustment<'tcx> {
283283
AdjustReifyFnPointer(ast::DefId), // go from a fn-item type to a fn-pointer type
284+
AdjustUnsafeFnPointer, // go from a safe fn pointer to an unsafe fn pointer
284285
AdjustDerefRef(AutoDerefRef<'tcx>)
285286
}
286287

@@ -2634,6 +2635,17 @@ impl<'tcx> ctxt<'tcx> {
26342635
substs
26352636
}
26362637

2638+
/// Create an unsafe fn ty based on a safe fn ty.
2639+
pub fn safe_to_unsafe_fn_ty(&self, bare_fn: &BareFnTy<'tcx>) -> Ty<'tcx> {
2640+
assert_eq!(bare_fn.unsafety, ast::Unsafety::Normal);
2641+
let unsafe_fn_ty_a = self.mk_bare_fn(ty::BareFnTy {
2642+
unsafety: ast::Unsafety::Unsafe,
2643+
abi: bare_fn.abi,
2644+
sig: bare_fn.sig.clone()
2645+
});
2646+
ty::mk_bare_fn(self, None, unsafe_fn_ty_a)
2647+
}
2648+
26372649
pub fn mk_bare_fn(&self, bare_fn: BareFnTy<'tcx>) -> &'tcx BareFnTy<'tcx> {
26382650
if let Some(bare_fn) = self.bare_fn_interner.borrow().get(&bare_fn) {
26392651
return *bare_fn;
@@ -4523,6 +4535,18 @@ pub fn adjust_ty<'tcx, F>(cx: &ctxt<'tcx>,
45234535
}
45244536
}
45254537

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

@@ -6685,6 +6709,7 @@ impl<'tcx> AutoAdjustment<'tcx> {
66856709
pub fn is_identity(&self) -> bool {
66866710
match *self {
66876711
AdjustReifyFnPointer(..) => false,
6712+
AdjustUnsafeFnPointer(..) => false,
66886713
AdjustDerefRef(ref r) => r.is_identity(),
66896714
}
66906715
}
@@ -6834,6 +6859,9 @@ impl<'tcx> Repr<'tcx> for AutoAdjustment<'tcx> {
68346859
AdjustReifyFnPointer(def_id) => {
68356860
format!("AdjustReifyFnPointer({})", def_id.repr(tcx))
68366861
}
6862+
AdjustUnsafeFnPointer => {
6863+
format!("AdjustUnsafeFnPointer")
6864+
}
68376865
AdjustDerefRef(ref data) => {
68386866
data.repr(tcx)
68396867
}

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.

0 commit comments

Comments
 (0)