Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_hir_typeck/src/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
// Commit the autoderefs by calling `autoderef` again, but this
// time writing the results into the various typeck results.
let mut autoderef = self.autoderef(self.call_expr.span, unadjusted_self_ty);
let Some((ty, n)) = autoderef.nth(pick.autoderefs) else {
let Some((mut target, n)) = autoderef.nth(pick.autoderefs) else {
return Ty::new_error_with_message(
self.tcx,
DUMMY_SP,
Expand All @@ -182,7 +182,6 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
assert_eq!(n, pick.autoderefs);

let mut adjustments = self.adjust_steps(&autoderef);
let mut target = self.structurally_resolve_type(autoderef.span(), ty);

match pick.autoref_or_ptr_adjustment {
Some(probe::AutorefOrPtrAdjustment::Autoref { mutbl, unsize }) => {
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_hir_typeck/src/place_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
result
}

fn negative_index(
fn negative_index_err(
&self,
ty: Ty<'tcx>,
span: Span,
base_expr: &hir::Expr<'_>,
) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
) -> (Ty<'tcx>, Ty<'tcx>) {
let ty = self.resolve_vars_if_possible(ty);
let mut err = self.dcx().struct_span_err(
span,
Expand All @@ -92,8 +92,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
}
let reported = err.emit();
Some((Ty::new_error(self.tcx, reported), Ty::new_error(self.tcx, reported)))
let guar = err.emit();
(Ty::new_error(self.tcx, guar), Ty::new_error(self.tcx, guar))
}

/// To type-check `base_expr[index_expr]`, we progressively autoderef
Expand All @@ -109,7 +109,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
index_ty: Ty<'tcx>,
index_expr: &hir::Expr<'_>,
) -> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)> {
let adjusted_ty = self.structurally_resolve_type(autoderef.span(), autoderef.final_ty());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means we no longer eagerly error on infer vars here. surprised we don't have a test for it. needs test + check for infer

Copy link
Contributor Author

@lcnr lcnr Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think

fn foo() {
    let x = &Default::default();
    x[1];
    let _: &Vec<()> = x;
}

would work

let adjusted_ty = autoderef.final_ty();
debug!(
"try_index_step(expr={:?}, base_expr={:?}, adjusted_ty={:?}, \
index_ty={:?})",
Expand All @@ -126,10 +126,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
match adjusted_ty.kind() {
ty::Adt(def, _) if self.tcx.is_diagnostic_item(sym::Vec, def.did()) => {
return self.negative_index(adjusted_ty, index_expr.span, base_expr);
return Some(self.negative_index_err(adjusted_ty, index_expr.span, base_expr));
}
ty::Slice(_) | ty::Array(_, _) => {
return self.negative_index(adjusted_ty, index_expr.span, base_expr);
return Some(self.negative_index_err(adjusted_ty, index_expr.span, base_expr));
}
_ => {}
}
Expand Down
Loading