Skip to content

Commit 1cd3d2f

Browse files
authored
Rollup merge of #41578 - arielb1:missing-adjustment, r=eddyb
typeck: resolve type vars before calling `try_index_step` `try_index_step` does not resolve type variables by itself and would fail otherwise. Also harden the failure path in `confirm` to cause less confusing errors. r? @eddyb Fixes #41498. beta-nominating because regression (caused by #41279).
2 parents 6fe2c24 + a260df2 commit 1cd3d2f

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

src/librustc/middle/mem_categorization.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
451451
// So peel off one-level, turning the &T into T.
452452
match base_ty.builtin_deref(false, ty::NoPreference) {
453453
Some(t) => t.ty,
454-
None => { return Err(()); }
454+
None => {
455+
debug!("By-ref binding of non-derefable type {:?}", base_ty);
456+
return Err(());
457+
}
455458
}
456459
}
457460
_ => base_ty,
@@ -1039,6 +1042,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
10391042
match base_cmt.ty.builtin_index() {
10401043
Some(ty) => (ty, ElementKind::VecElement),
10411044
None => {
1045+
debug!("Explicit index of non-indexable type {:?}", base_cmt);
10421046
return Err(());
10431047
}
10441048
}
@@ -1154,7 +1158,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11541158
PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
11551159
PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
11561160
match path.def {
1157-
Def::Err => return Err(()),
1161+
Def::Err => {
1162+
debug!("access to unresolvable pattern {:?}", pat);
1163+
return Err(())
1164+
}
11581165
Def::Variant(variant_did) |
11591166
Def::VariantCtor(variant_did, ..) => {
11601167
// univariant enums do not need downcasts

src/librustc_errors/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ impl Handler {
375375
panic!(ExplicitBug);
376376
}
377377
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
378+
if self.treat_err_as_bug {
379+
self.span_bug(sp, msg);
380+
}
378381
let mut delayed = self.delayed_span_bug.borrow_mut();
379382
*delayed = Some((sp.into(), msg.to_string()));
380383
}

src/librustc_typeck/check/method/confirm.rs

+11
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
516516
};
517517

518518
let index_expr_ty = self.node_ty(index_expr.id);
519+
let adjusted_base_ty = self.resolve_type_vars_if_possible(&adjusted_base_ty);
520+
let index_expr_ty = self.resolve_type_vars_if_possible(&index_expr_ty);
519521

520522
let result = self.try_index_step(ty::MethodCall::expr(expr.id),
521523
expr,
@@ -531,6 +533,15 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
531533

532534
let expr_ty = self.node_ty(expr.id);
533535
self.demand_suptype(expr.span, expr_ty, return_ty);
536+
} else {
537+
// We could not perform a mutable index. Re-apply the
538+
// immutable index adjustments - borrowck will detect
539+
// this as an error.
540+
if let Some(adjustment) = adjustment {
541+
self.apply_adjustment(expr.id, adjustment);
542+
}
543+
self.tcx.sess.delay_span_bug(
544+
expr.span, "convert_lvalue_derefs_to_mutable failed");
534545
}
535546
}
536547
hir::ExprUnary(hir::UnDeref, ref base_expr) => {

src/test/run-pass/issue-41498.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
// regression test for issue #41498.
12+
13+
struct S;
14+
impl S {
15+
fn mutate(&mut self) {}
16+
}
17+
18+
fn call_and_ref<T, F: FnOnce() -> T>(x: &mut Option<T>, f: F) -> &mut T {
19+
*x = Some(f());
20+
x.as_mut().unwrap()
21+
}
22+
23+
fn main() {
24+
let mut n = None;
25+
call_and_ref(&mut n, || [S])[0].mutate();
26+
}

0 commit comments

Comments
 (0)