Skip to content

Commit 71b21ed

Browse files
committed
Auto merge of #46253 - eddyb:return-aliasing, r=nagisa
rustc_trans: don't apply noalias on returned references. In #45225 frozen returned `&T` were accidentally maked `noalias`, unlike `&mut T`. Return value `noalias` is only sound for functions that return dynamic allocations, e.g. `Box`, and using it on anything else can lead to miscompilation, as LLVM assumes certain usage patterns. Fixes #46239.
2 parents 0400312 + 5eed95e commit 71b21ed

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/librustc_trans/abi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,8 @@ impl<'a, 'tcx> FnType<'tcx> {
792792
// dependencies rather than pointer equality
793793
let no_alias = match kind {
794794
PointerKind::Shared => false,
795-
PointerKind::Frozen | PointerKind::UniqueOwned => true,
795+
PointerKind::UniqueOwned => true,
796+
PointerKind::Frozen |
796797
PointerKind::UniqueBorrowed => !is_return
797798
};
798799
if no_alias {
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) main.rs -C opt-level=1
5+
$(call RUN,main)

src/test/run-make/issue-46239/main.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 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+
fn project<T>(x: &(T,)) -> &T { &x.0 }
12+
13+
fn dummy() {}
14+
15+
fn main() {
16+
let f = (dummy as fn(),);
17+
(*project(&f))();
18+
}

0 commit comments

Comments
 (0)