Skip to content

Commit 2fa8790

Browse files
committed
Auto merge of #63498 - Mark-Simulacrum:stable-next, r=Mark-Simulacrum
1.37.0 stable This promotes beta to stable and backports a few PRs: - Avoid ICE when referencing desugared local binding in borrow error (#63051) - Don't access a static just for its size and alignment (#62982) via 331e09b143aebfcf82dc1f9b69b31ee0083cbf0b
2 parents 3f55461 + c9be294 commit 2fa8790

File tree

8 files changed

+108
-36
lines changed

8 files changed

+108
-36
lines changed

src/ci/azure-pipelines/steps/run.yml

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ steps:
3030
- bash: printenv | sort
3131
displayName: Show environment variables
3232

33+
# Log the date, even on failure. Attempting to debug SSL certificate errors.
34+
- bash: date
35+
displayName: Print out date (before build)
36+
3337
- bash: |
3438
set -e
3539
df -h
@@ -198,3 +202,8 @@ steps:
198202
condition: variables['AWS_SECRET_ACCESS_KEY']
199203
continueOnError: true
200204
displayName: Upload CPU usage statistics
205+
206+
# Log the date, even on failure. Attempting to debug SSL certificate errors.
207+
- bash: date
208+
continueOnError: true
209+
displayName: Print out date (after build)

src/ci/run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fi
4545
#
4646
# FIXME: need a scheme for changing this `nightly` value to `beta` and `stable`
4747
# either automatically or manually.
48-
export RUST_RELEASE_CHANNEL=beta
48+
export RUST_RELEASE_CHANNEL=stable
4949
if [ "$DEPLOY$DEPLOY_ALT" = "1" ]; then
5050
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --release-channel=$RUST_RELEASE_CHANNEL"
5151
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --enable-llvm-static-stdcpp"

src/librustc_mir/borrow_check/conflict_errors.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1116,19 +1116,18 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11161116
bug!("try_report_cannot_return_reference_to_local: not a local")
11171117
};
11181118
match self.body.local_kind(local) {
1119-
LocalKind::ReturnPointer | LocalKind::Temp => {
1120-
(
1121-
"temporary value".to_string(),
1122-
"temporary value created here".to_string(),
1123-
)
1124-
}
1125-
LocalKind::Arg => {
1126-
(
1127-
"function parameter".to_string(),
1128-
"function parameter borrowed here".to_string(),
1129-
)
1130-
},
1131-
LocalKind::Var => bug!("local variable without a name"),
1119+
LocalKind::ReturnPointer | LocalKind::Temp => (
1120+
"temporary value".to_string(),
1121+
"temporary value created here".to_string(),
1122+
),
1123+
LocalKind::Arg => (
1124+
"function parameter".to_string(),
1125+
"function parameter borrowed here".to_string(),
1126+
),
1127+
LocalKind::Var => (
1128+
"local binding".to_string(),
1129+
"local binding introduced here".to_string(),
1130+
),
11321131
}
11331132
};
11341133

src/librustc_mir/interpret/machine.rs

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ pub trait AllocMap<K: Hash + Eq, V> {
5454
k: K,
5555
vacant: impl FnOnce() -> Result<V, E>
5656
) -> Result<&mut V, E>;
57+
58+
/// Read-only lookup.
59+
fn get(&self, k: K) -> Option<&V> {
60+
self.get_or(k, || Err(())).ok()
61+
}
5762
}
5863

5964
/// Methods of this trait signifies a point where CTFE evaluation would fail

src/librustc_mir/interpret/memory.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,18 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
492492
id: AllocId,
493493
liveness: AllocCheck,
494494
) -> InterpResult<'static, (Size, Align)> {
495-
if let Ok(alloc) = self.get(id) {
495+
// # Regular allocations
496+
// Don't use `self.get` here as that will
497+
// a) cause cycles in case `id` refers to a static
498+
// b) duplicate a static's allocation in miri
499+
if let Some((_, alloc)) = self.alloc_map.get(id) {
496500
return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
497501
}
498-
// can't do this in the match argument, we may get cycle errors since the lock would get
499-
// dropped after the match.
502+
503+
// # Statics and function pointers
504+
// Can't do this in the match argument, we may get cycle errors since the lock would
505+
// be held throughout the match.
500506
let alloc = self.tcx.alloc_map.lock().get(id);
501-
// Could also be a fn ptr or extern static
502507
match alloc {
503508
Some(GlobalAlloc::Function(..)) => {
504509
if let AllocCheck::Dereferencable = liveness {
@@ -507,28 +512,26 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
507512
} else {
508513
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
509514
}
510-
}
511-
// `self.get` would also work, but can cause cycles if a static refers to itself
515+
},
512516
Some(GlobalAlloc::Static(did)) => {
513-
// The only way `get` couldn't have worked here is if this is an extern static
514-
assert!(self.tcx.is_foreign_item(did));
515-
// Use size and align of the type
517+
// Use size and align of the type.
516518
let ty = self.tcx.type_of(did);
517519
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
518520
Ok((layout.size, layout.align.abi))
519-
}
520-
_ => {
521-
if let Ok(alloc) = self.get(id) {
522-
Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align))
523-
}
524-
else if let AllocCheck::MaybeDead = liveness {
525-
// Deallocated pointers are allowed, we should be able to find
526-
// them in the map.
527-
Ok(*self.dead_alloc_map.get(&id)
528-
.expect("deallocated pointers should all be recorded in `dead_alloc_map`"))
529-
} else {
530-
err!(DanglingPointerDeref)
531-
}
521+
},
522+
Some(GlobalAlloc::Memory(alloc)) =>
523+
// Need to duplicate the logic here, because the global allocations have
524+
// different associated types than the interpreter-local ones.
525+
Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align)),
526+
// The rest must be dead.
527+
None => if let AllocCheck::MaybeDead = liveness {
528+
// Deallocated pointers are allowed, we should be able to find
529+
// them in the map.
530+
Ok(*self.dead_alloc_map.get(&id)
531+
.expect("deallocated pointers should all be recorded in \
532+
`dead_alloc_map`"))
533+
} else {
534+
err!(DanglingPointerDeref)
532535
},
533536
}
534537
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// To avoid leaking the names of local bindings from expressions like for loops, #60984
2+
// explicitly ignored them, but an assertion that `LocalKind::Var` *must* have a name would
3+
// trigger an ICE. Before this change, this file's output would be:
4+
// ```
5+
// error[E0515]: cannot return value referencing local variable `__next`
6+
// --> return-local-binding-from-desugaring.rs:LL:CC
7+
// |
8+
// LL | for ref x in xs {
9+
// | ----- `__next` is borrowed here
10+
// ...
11+
// LL | result
12+
// | ^^^^^^ returns a value referencing data owned by the current function
13+
// ```
14+
// FIXME: ideally `LocalKind` would carry more information to more accurately explain the problem.
15+
16+
use std::collections::HashMap;
17+
use std::hash::Hash;
18+
19+
fn group_by<I, F, T>(xs: &mut I, f: F) -> HashMap<T, Vec<&I::Item>>
20+
where
21+
I: Iterator,
22+
F: Fn(&I::Item) -> T,
23+
T: Eq + Hash,
24+
{
25+
let mut result = HashMap::new();
26+
for ref x in xs {
27+
let key = f(x);
28+
result.entry(key).or_insert(Vec::new()).push(x);
29+
}
30+
result //~ ERROR cannot return value referencing local binding
31+
}
32+
33+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0515]: cannot return value referencing local binding
2+
--> $DIR/return-local-binding-from-desugaring.rs:30:5
3+
|
4+
LL | for ref x in xs {
5+
| -- local binding introduced here
6+
...
7+
LL | result
8+
| ^^^^^^ returns a value referencing data owned by the current function
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0515`.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-pass
2+
3+
struct Foo {
4+
foo: Option<&'static Foo>
5+
}
6+
7+
static FOO: Foo = Foo {
8+
foo: Some(&FOO),
9+
};
10+
11+
fn main() {}

0 commit comments

Comments
 (0)