Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix built-in indexing not being used where index type wasn't "obviously" usize #47167

Merged
merged 7 commits into from
Jan 10, 2018
Merged
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
12 changes: 0 additions & 12 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,18 +2217,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
adjusted_ty,
index_ty);

// First, try built-in indexing.
match (adjusted_ty.builtin_index(), &index_ty.sty) {
(Some(ty), &ty::TyUint(ast::UintTy::Usize)) |
(Some(ty), &ty::TyInfer(ty::IntVar(_))) => {
debug!("try_index_step: success, using built-in indexing");
let adjustments = autoderef.adjust_steps(lvalue_pref);
self.apply_adjustments(base_expr, adjustments);
return Some((self.tcx.types.usize, ty));
}
_ => {}
}

for &unsize in &[false, true] {
let mut self_ty = adjusted_ty;
if unsize {
Expand Down
46 changes: 46 additions & 0 deletions src/librustc_typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc::hir::def_id::{DefId, DefIndex};
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc::infer::InferCtxt;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::adjustment::{Adjust, Adjustment};
use rustc::ty::fold::{TypeFoldable, TypeFolder};
use rustc::util::nodemap::DefIdSet;
use syntax::ast;
Expand Down Expand Up @@ -159,8 +160,52 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
_ => {}
}
}

// Similar to operators, indexing is always assumed to be overloaded
// Here, correct cases where an indexing expression can be simplified
// to use builtin indexing because the index type is known to be
// usize-ish
fn fix_index_builtin_expr(&mut self, e: &hir::Expr) {
if let hir::ExprIndex(ref base, ref index) = e.node {
let mut tables = self.fcx.tables.borrow_mut();

match tables.expr_ty_adjusted(&base).sty {
// All valid indexing looks like this
ty::TyRef(_, ty::TypeAndMut { ty: ref base_ty, .. }) => {
let index_ty = tables.expr_ty_adjusted(&index);
let index_ty = self.fcx.resolve_type_vars_if_possible(&index_ty);

if base_ty.builtin_index().is_some()
&& index_ty == self.fcx.tcx.types.usize {
// Remove the method call record
tables.type_dependent_defs_mut().remove(e.hir_id);
tables.node_substs_mut().remove(e.hir_id);

tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
// Discard the need for a mutable borrow
match a.pop() {
// Extra adjustment made when indexing causes a drop
// of size information - we need to get rid of it
// Since this is "after" the other adjustment to be
// discarded, we do an extra `pop()`
Some(Adjustment { kind: Adjust::Unsize, .. }) => {
// So the borrow discard actually happens here
a.pop();
},
_ => {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some kind of assertion here? That is, the adjustment we are popping, will it always be a ref adjustment or something similar?

Copy link
Contributor

Choose a reason for hiding this comment

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

Eh, looking at the other cases, this looks similarly paranoid (or not paranoid). Seems fine.

}
});
}
},
// Might encounter non-valid indexes at this point, so there
// has to be a fall-through
_ => {},
}
}
}
}


///////////////////////////////////////////////////////////////////////////
// Impl of Visitor for Resolver
//
Expand All @@ -176,6 +221,7 @@ impl<'cx, 'gcx, 'tcx> Visitor<'gcx> for WritebackCx<'cx, 'gcx, 'tcx> {

fn visit_expr(&mut self, e: &'gcx hir::Expr) {
self.fix_scalar_builtin_expr(e);
self.fix_index_builtin_expr(e);

self.visit_node_id(e.span, e.hir_id);

Expand Down
19 changes: 19 additions & 0 deletions src/test/run-pass/issue-33903.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Issue 33903:
// Built-in indexing should be used even when the index is not
// trivially an integer
// Only built-in indexing can be used in constant expresssions

const FOO: i32 = [12, 34][0 + 1];

fn main() {}

39 changes: 39 additions & 0 deletions src/test/run-pass/issue-46095.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct A;

impl A {
fn take_mutably(&mut self) {}
}

fn identity<T>(t: T) -> T {
t
}

// Issue 46095
// Built-in indexing should be used even when the index is not
// trivially an integer
// Overloaded indexing would cause wrapped to be borrowed mutably

fn main() {
let mut a1 = A;
let mut a2 = A;

let wrapped = [&mut a1, &mut a2];

{
wrapped[0 + 1 - 1].take_mutably();
}

{
wrapped[identity(0)].take_mutably();
}
}