Skip to content

Commit 1d33318

Browse files
committed
Auto merge of #26336 - dotdash:raw_ptr_coercions, r=nrc
Unlike coercing from reference to unsafe pointer, coercing between two unsafe pointers doesn't need an AutoDerefRef, because there is no region that regionck would need to know about. In unoptimized libcore, this reduces the number of "auto_deref" allocas from 174 to 4.
2 parents 20d23d8 + cabd068 commit 1d33318

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/librustc_typeck/check/coercion.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
403403
a.repr(self.tcx()),
404404
b.repr(self.tcx()));
405405

406-
let mt_a = match a.sty {
407-
ty::TyRef(_, mt) | ty::TyRawPtr(mt) => mt,
406+
let (is_ref, mt_a) = match a.sty {
407+
ty::TyRef(_, mt) => (true, mt),
408+
ty::TyRawPtr(mt) => (false, mt),
408409
_ => {
409410
return self.subtype(a, b);
410411
}
@@ -418,11 +419,15 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
418419
// Although references and unsafe ptrs have the same
419420
// representation, we still register an AutoDerefRef so that
420421
// regionck knows that the region for `a` must be valid here.
421-
Ok(Some(AdjustDerefRef(AutoDerefRef {
422-
autoderefs: 1,
423-
autoref: Some(ty::AutoUnsafe(mutbl_b)),
424-
unsize: None
425-
})))
422+
if is_ref {
423+
Ok(Some(AdjustDerefRef(AutoDerefRef {
424+
autoderefs: 1,
425+
autoref: Some(ty::AutoUnsafe(mutbl_b)),
426+
unsize: None
427+
})))
428+
} else {
429+
Ok(None)
430+
}
426431
}
427432
}
428433

src/test/codegen/coercions.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2015 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+
// compile-flags: -C no-prepopulate-passes
12+
13+
static X: i32 = 5;
14+
15+
// CHECK-LABEL: @raw_ptr_to_raw_ptr_noop
16+
// CHECK-NOT: alloca
17+
#[no_mangle]
18+
pub fn raw_ptr_to_raw_ptr_noop() -> *const i32{
19+
&X as *const i32
20+
}
21+
22+
// CHECK-LABEL: @reference_to_raw_ptr_noop
23+
// CHECK-NOT: alloca
24+
#[no_mangle]
25+
pub fn reference_to_raw_ptr_noop() -> *const i32 {
26+
&X
27+
}

src/test/compile-fail/issue-16538.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod Y {
2020

2121
static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
2222
//~^ ERROR the trait `core::marker::Sync` is not implemented for the type
23+
//~| ERROR cannot refer to other statics by value, use the address-of operator or a constant instead
2324
//~| ERROR E0015
2425

2526
fn main() {}

0 commit comments

Comments
 (0)