Skip to content

Commit 5bc9807

Browse files
committed
Auto merge of #91093 - matthiaskrgr:rollup-kovzwx0, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #89741 (Mark `Arc::from_inner` / `Rc::from_inner` as unsafe) - #90927 (Fix float ICE) - #90994 (Fix ICE `#90993`: add missing call to cancel) - #91018 (Adopt let_else in more places in rustc_mir_build) - #91022 (Suggest `await` in more situations where infer types are involved) - #91088 (Revert "require full validity when determining the discriminant of a value") Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2885c47 + 83c83d4 commit 5bc9807

File tree

352 files changed

+263
-119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

352 files changed

+263
-119
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

-6
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
265265
}
266266
sym::discriminant_value => {
267267
let place = self.deref_operand(&args[0])?;
268-
if M::enforce_validity(self) {
269-
// This is 'using' the value, so make sure the validity invariant is satisfied.
270-
// (Also see https://github.com/rust-lang/rust/pull/89764.)
271-
self.validate_operand(&place.into())?;
272-
}
273-
274268
let discr_val = self.read_discriminant(&place.into())?.0;
275269
self.write_scalar(discr_val, dest)?;
276270
}

compiler/rustc_const_eval/src/interpret/step.rs

-6
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
304304

305305
Discriminant(place) => {
306306
let op = self.eval_place_to_op(place, None)?;
307-
if M::enforce_validity(self) {
308-
// This is 'using' the value, so make sure the validity invariant is satisfied.
309-
// (Also see https://github.com/rust-lang/rust/pull/89764.)
310-
self.validate_operand(&op)?;
311-
}
312-
313307
let discr_val = self.read_discriminant(&op)?.0;
314308
self.write_scalar(discr_val, &dest)?;
315309
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+31-3
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,34 @@ pub fn unexpected_hidden_region_diagnostic(
310310
err
311311
}
312312

313+
/// Structurally compares two types, modulo any inference variables.
314+
///
315+
/// Returns `true` if two types are equal, or if one type is an inference variable compatible
316+
/// with the other type. A TyVar inference type is compatible with any type, and an IntVar or
317+
/// FloatVar inference type are compatible with themselves or their concrete types (Int and
318+
/// Float types, respectively). When comparing two ADTs, these rules apply recursively.
319+
pub fn same_type_modulo_infer(a: Ty<'tcx>, b: Ty<'ctx>) -> bool {
320+
match (&a.kind(), &b.kind()) {
321+
(&ty::Adt(did_a, substs_a), &ty::Adt(did_b, substs_b)) => {
322+
if did_a != did_b {
323+
return false;
324+
}
325+
326+
substs_a.types().zip(substs_b.types()).all(|(a, b)| same_type_modulo_infer(a, b))
327+
}
328+
(&ty::Int(_), &ty::Infer(ty::InferTy::IntVar(_)))
329+
| (&ty::Infer(ty::InferTy::IntVar(_)), &ty::Int(_) | &ty::Infer(ty::InferTy::IntVar(_)))
330+
| (&ty::Float(_), &ty::Infer(ty::InferTy::FloatVar(_)))
331+
| (
332+
&ty::Infer(ty::InferTy::FloatVar(_)),
333+
&ty::Float(_) | &ty::Infer(ty::InferTy::FloatVar(_)),
334+
)
335+
| (&ty::Infer(ty::InferTy::TyVar(_)), _)
336+
| (_, &ty::Infer(ty::InferTy::TyVar(_))) => true,
337+
_ => a == b,
338+
}
339+
}
340+
313341
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
314342
pub fn report_region_errors(&self, errors: &Vec<RegionResolutionError<'tcx>>) {
315343
debug!("report_region_errors(): {} errors to start", errors.len());
@@ -1761,7 +1789,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17611789
self.get_impl_future_output_ty(exp_found.expected),
17621790
self.get_impl_future_output_ty(exp_found.found),
17631791
) {
1764-
(Some(exp), Some(found)) if ty::TyS::same_type(exp, found) => match &cause.code {
1792+
(Some(exp), Some(found)) if same_type_modulo_infer(exp, found) => match &cause.code {
17651793
ObligationCauseCode::IfExpression(box IfExpressionCause { then, .. }) => {
17661794
diag.multipart_suggestion(
17671795
"consider `await`ing on both `Future`s",
@@ -1793,15 +1821,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17931821
diag.help("consider `await`ing on both `Future`s");
17941822
}
17951823
},
1796-
(_, Some(ty)) if ty::TyS::same_type(exp_found.expected, ty) => {
1824+
(_, Some(ty)) if same_type_modulo_infer(exp_found.expected, ty) => {
17971825
diag.span_suggestion_verbose(
17981826
exp_span.shrink_to_hi(),
17991827
"consider `await`ing on the `Future`",
18001828
".await".to_string(),
18011829
Applicability::MaybeIncorrect,
18021830
);
18031831
}
1804-
(Some(ty), _) if ty::TyS::same_type(ty, exp_found.found) => match cause.code {
1832+
(Some(ty), _) if same_type_modulo_infer(ty, exp_found.found) => match cause.code {
18051833
ObligationCauseCode::Pattern { span: Some(span), .. }
18061834
| ObligationCauseCode::IfExpression(box IfExpressionCause { then: span, .. }) => {
18071835
diag.span_suggestion_verbose(

compiler/rustc_mir_build/src/build/matches/mod.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1606,13 +1606,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16061606
// encounter a candidate where the test is not relevant; at
16071607
// that point, we stop sorting.
16081608
while let Some(candidate) = candidates.first_mut() {
1609-
if let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) {
1610-
let (candidate, rest) = candidates.split_first_mut().unwrap();
1611-
target_candidates[idx].push(candidate);
1612-
candidates = rest;
1613-
} else {
1609+
let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else {
16141610
break;
1615-
}
1611+
};
1612+
let (candidate, rest) = candidates.split_first_mut().unwrap();
1613+
target_candidates[idx].push(candidate);
1614+
candidates = rest;
16161615
}
16171616
// at least the first candidate ought to be tested
16181617
assert!(total_candidate_count > candidates.len());

compiler/rustc_mir_build/src/build/mod.rs

+50-51
Original file line numberDiff line numberDiff line change
@@ -966,59 +966,58 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
966966
DropKind::Value,
967967
);
968968

969-
if let Some(arg) = arg_opt {
970-
let pat = match tcx.hir().get(arg.pat.hir_id) {
971-
Node::Pat(pat) | Node::Binding(pat) => pat,
972-
node => bug!("pattern became {:?}", node),
973-
};
974-
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
975-
let original_source_scope = self.source_scope;
976-
let span = pattern.span;
977-
self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
978-
match *pattern.kind {
979-
// Don't introduce extra copies for simple bindings
980-
PatKind::Binding {
981-
mutability,
982-
var,
983-
mode: BindingMode::ByValue,
984-
subpattern: None,
985-
..
986-
} => {
987-
self.local_decls[local].mutability = mutability;
988-
self.local_decls[local].source_info.scope = self.source_scope;
989-
self.local_decls[local].local_info = if let Some(kind) = self_binding {
990-
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
991-
BindingForm::ImplicitSelf(*kind),
992-
))))
993-
} else {
994-
let binding_mode = ty::BindingMode::BindByValue(mutability);
995-
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
996-
VarBindingForm {
997-
binding_mode,
998-
opt_ty_info,
999-
opt_match_place: Some((Some(place), span)),
1000-
pat_span: span,
1001-
},
1002-
)))))
1003-
};
1004-
self.var_indices.insert(var, LocalsForNode::One(local));
1005-
}
1006-
_ => {
1007-
scope = self.declare_bindings(
1008-
scope,
1009-
expr.span,
1010-
&pattern,
1011-
matches::ArmHasGuard(false),
1012-
Some((Some(&place), span)),
1013-
);
1014-
let place_builder = PlaceBuilder::from(local);
1015-
unpack!(
1016-
block = self.place_into_pattern(block, pattern, place_builder, false)
1017-
);
1018-
}
969+
let Some(arg) = arg_opt else {
970+
continue;
971+
};
972+
let pat = match tcx.hir().get(arg.pat.hir_id) {
973+
Node::Pat(pat) | Node::Binding(pat) => pat,
974+
node => bug!("pattern became {:?}", node),
975+
};
976+
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
977+
let original_source_scope = self.source_scope;
978+
let span = pattern.span;
979+
self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
980+
match *pattern.kind {
981+
// Don't introduce extra copies for simple bindings
982+
PatKind::Binding {
983+
mutability,
984+
var,
985+
mode: BindingMode::ByValue,
986+
subpattern: None,
987+
..
988+
} => {
989+
self.local_decls[local].mutability = mutability;
990+
self.local_decls[local].source_info.scope = self.source_scope;
991+
self.local_decls[local].local_info = if let Some(kind) = self_binding {
992+
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
993+
BindingForm::ImplicitSelf(*kind),
994+
))))
995+
} else {
996+
let binding_mode = ty::BindingMode::BindByValue(mutability);
997+
Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
998+
VarBindingForm {
999+
binding_mode,
1000+
opt_ty_info,
1001+
opt_match_place: Some((Some(place), span)),
1002+
pat_span: span,
1003+
},
1004+
)))))
1005+
};
1006+
self.var_indices.insert(var, LocalsForNode::One(local));
1007+
}
1008+
_ => {
1009+
scope = self.declare_bindings(
1010+
scope,
1011+
expr.span,
1012+
&pattern,
1013+
matches::ArmHasGuard(false),
1014+
Some((Some(&place), span)),
1015+
);
1016+
let place_builder = PlaceBuilder::from(local);
1017+
unpack!(block = self.place_into_pattern(block, pattern, place_builder, false));
10191018
}
1020-
self.source_scope = original_source_scope;
10211019
}
1020+
self.source_scope = original_source_scope;
10221021
}
10231022

10241023
// Enter the argument pattern bindings source scope, if it exists.

compiler/rustc_mir_build/src/check_unsafety.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -256,23 +256,22 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
256256
}
257257
PatKind::Binding { mode: BindingMode::ByRef(borrow_kind), ty, .. } => {
258258
if self.inside_adt {
259-
if let ty::Ref(_, ty, _) = ty.kind() {
260-
match borrow_kind {
261-
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
262-
if !ty.is_freeze(self.tcx.at(pat.span), self.param_env) {
263-
self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField);
264-
}
265-
}
266-
BorrowKind::Mut { .. } => {
267-
self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField);
268-
}
269-
}
270-
} else {
259+
let ty::Ref(_, ty, _) = ty.kind() else {
271260
span_bug!(
272261
pat.span,
273262
"BindingMode::ByRef in pattern, but found non-reference type {}",
274263
ty
275264
);
265+
};
266+
match borrow_kind {
267+
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
268+
if !ty.is_freeze(self.tcx.at(pat.span), self.param_env) {
269+
self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField);
270+
}
271+
}
272+
BorrowKind::Mut { .. } => {
273+
self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField);
274+
}
276275
}
277276
}
278277
visit::walk_pat(self, pat);

compiler/rustc_parse/src/parser/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,8 @@ impl<'a> Parser<'a> {
10321032
[IdentLike(_), Punct('+' | '-')] |
10331033
// 1e+2 | 1e-2
10341034
[IdentLike(_), Punct('+' | '-'), IdentLike(_)] |
1035+
// 1.2e+ | 1.2e-
1036+
[IdentLike(_), Punct('.'), IdentLike(_), Punct('+' | '-')] |
10351037
// 1.2e+3 | 1.2e-3
10361038
[IdentLike(_), Punct('.'), IdentLike(_), Punct('+' | '-'), IdentLike(_)] => {
10371039
// See the FIXME about `TokenCursor` above.

compiler/rustc_parse/src/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ impl<'a> Parser<'a> {
817817
// Ensure the user doesn't receive unhelpful unexpected token errors
818818
self.bump();
819819
if self.is_pat_range_end_start(0) {
820-
let _ = self.parse_pat_range_end();
820+
let _ = self.parse_pat_range_end().map_err(|mut e| e.cancel());
821821
}
822822

823823
self.error_inclusive_range_with_extra_equals(span_with_eq);

library/alloc/src/rc.rs

+31-20
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,12 @@ impl<T: ?Sized> Rc<T> {
341341
unsafe { self.ptr.as_ref() }
342342
}
343343

344-
fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
344+
unsafe fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
345345
Self { ptr, phantom: PhantomData }
346346
}
347347

348348
unsafe fn from_ptr(ptr: *mut RcBox<T>) -> Self {
349-
Self::from_inner(unsafe { NonNull::new_unchecked(ptr) })
349+
unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
350350
}
351351
}
352352

@@ -367,9 +367,11 @@ impl<T> Rc<T> {
367367
// pointers, which ensures that the weak destructor never frees
368368
// the allocation while the strong destructor is running, even
369369
// if the weak pointer is stored inside the strong one.
370-
Self::from_inner(
371-
Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
372-
)
370+
unsafe {
371+
Self::from_inner(
372+
Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
373+
)
374+
}
373375
}
374376

375377
/// Constructs a new `Rc<T>` using a weak reference to itself. Attempting
@@ -420,16 +422,16 @@ impl<T> Rc<T> {
420422
// otherwise.
421423
let data = data_fn(&weak);
422424

423-
unsafe {
425+
let strong = unsafe {
424426
let inner = init_ptr.as_ptr();
425427
ptr::write(ptr::addr_of_mut!((*inner).value), data);
426428

427429
let prev_value = (*inner).strong.get();
428430
debug_assert_eq!(prev_value, 0, "No prior strong references should exist");
429431
(*inner).strong.set(1);
430-
}
431432

432-
let strong = Rc::from_inner(init_ptr);
433+
Rc::from_inner(init_ptr)
434+
};
433435

434436
// Strong references should collectively own a shared weak reference,
435437
// so don't run the destructor for our old weak reference.
@@ -521,10 +523,12 @@ impl<T> Rc<T> {
521523
// pointers, which ensures that the weak destructor never frees
522524
// the allocation while the strong destructor is running, even
523525
// if the weak pointer is stored inside the strong one.
524-
Ok(Self::from_inner(
525-
Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
526-
.into(),
527-
))
526+
unsafe {
527+
Ok(Self::from_inner(
528+
Box::leak(Box::try_new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value })?)
529+
.into(),
530+
))
531+
}
528532
}
529533

530534
/// Constructs a new `Rc` with uninitialized contents, returning an error if the allocation fails
@@ -746,7 +750,7 @@ impl<T> Rc<mem::MaybeUninit<T>> {
746750
#[unstable(feature = "new_uninit", issue = "63291")]
747751
#[inline]
748752
pub unsafe fn assume_init(self) -> Rc<T> {
749-
Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast())
753+
unsafe { Rc::from_inner(mem::ManuallyDrop::new(self).ptr.cast()) }
750754
}
751755
}
752756

@@ -1214,9 +1218,11 @@ impl Rc<dyn Any> {
12141218
/// ```
12151219
pub fn downcast<T: Any>(self) -> Result<Rc<T>, Rc<dyn Any>> {
12161220
if (*self).is::<T>() {
1217-
let ptr = self.ptr.cast::<RcBox<T>>();
1218-
forget(self);
1219-
Ok(Rc::from_inner(ptr))
1221+
unsafe {
1222+
let ptr = self.ptr.cast::<RcBox<T>>();
1223+
forget(self);
1224+
Ok(Rc::from_inner(ptr))
1225+
}
12201226
} else {
12211227
Err(self)
12221228
}
@@ -1489,8 +1495,10 @@ impl<T: ?Sized> Clone for Rc<T> {
14891495
/// ```
14901496
#[inline]
14911497
fn clone(&self) -> Rc<T> {
1492-
self.inner().inc_strong();
1493-
Self::from_inner(self.ptr)
1498+
unsafe {
1499+
self.inner().inc_strong();
1500+
Self::from_inner(self.ptr)
1501+
}
14941502
}
14951503
}
14961504

@@ -2245,11 +2253,14 @@ impl<T: ?Sized> Weak<T> {
22452253
#[stable(feature = "rc_weak", since = "1.4.0")]
22462254
pub fn upgrade(&self) -> Option<Rc<T>> {
22472255
let inner = self.inner()?;
2256+
22482257
if inner.strong() == 0 {
22492258
None
22502259
} else {
2251-
inner.inc_strong();
2252-
Some(Rc::from_inner(self.ptr))
2260+
unsafe {
2261+
inner.inc_strong();
2262+
Some(Rc::from_inner(self.ptr))
2263+
}
22532264
}
22542265
}
22552266

0 commit comments

Comments
 (0)