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

Check discriminant types before span_mirbug #100498

Closed
wants to merge 5 commits into from
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
49 changes: 48 additions & 1 deletion compiler/rustc_typeck/src/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::lang_items::LangItem;
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
use rustc_infer::infer::InferCtxt;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::hir::place::Place as HirPlace;
use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast};
Expand All @@ -19,6 +21,7 @@ use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable};
use rustc_middle::ty::{self, ClosureSizeProfileData, Ty, TyCtxt};
use rustc_span::symbol::sym;
use rustc_span::Span;
use rustc_trait_selection::traits::{self, ImplSource, SelectionContext};

use std::mem;
use std::ops::ControlFlow;
Expand Down Expand Up @@ -192,6 +195,48 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
}
}

// (ouz-a #100498): Normally `[T] : std::ops::Index<usize>` should be normalized
// into [T] but currently `Where` clause stops the normalization process for it,
// here we check if the expr is `LangItem::Index` and index_ty is `usize` if so we don't
// `fix_index_builtin_expr`.
fn is_builtin_index(
&mut self,
e: &hir::Expr<'_>,
base: Ty<'tcx>,
Copy link
Member

Choose a reason for hiding this comment

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

(nit, not needed) maybe just me, but I prefer Ty<'tcx> variables to be called something_ty

index: Ty<'tcx>,
base_span: Span,
index_span: Span,
) -> bool {
let mut ret = false;
Copy link
Member

Choose a reason for hiding this comment

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

Remove this in favor of return true or return false where needed.

if (&base).builtin_index().is_some() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (&base).builtin_index().is_some() {
if base.builtin_index().is_some() {

Also, we could invert this, like:

if base.builtin_index().is_none() {
  return false;
}

That saves us an indentation level.

let resolved_base_ty = self.resolve(base, &base_span);
let resolved_index_ty = self.resolve(index, &index_span);
self.tcx().infer_ctxt().enter(|infcx| {
let mut selection_context = SelectionContext::new(&infcx);
Copy link
Member

Choose a reason for hiding this comment

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

nit: I think it's conventional to call the selection context selcx


let def_id = self.tcx().require_lang_item(LangItem::Index, Some(e.span));
Copy link
Member

Choose a reason for hiding this comment

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

nit: We could probably use a let else here, and just return false; if this lang item is missing rather than erroring.

let substs = self
.tcx()
.mk_substs([resolved_base_ty.into(), resolved_index_ty.into()].iter());
let trait_ref = ty::TraitRef { def_id, substs };
let binder = ty::Binder::dummy(trait_ref).without_const();
let obligation = traits::Obligation::new(
traits::ObligationCause::dummy_with_span(e.span),
self.fcx.param_env,
binder,
);
let results = selection_context.select(&obligation);

if let Ok(Some(ImplSource::Param(..))) = results {
ret = false;
} else {
ret = true;
}
});
}
ret
Comment on lines +230 to +237
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if let Ok(Some(ImplSource::Param(..))) = results {
ret = false;
} else {
ret = true;
}
});
}
ret
// Due to coherence, the only `impl` that could apply is the built-in one
if let Ok(Some(ImplSource::UserDefined(..))) = results {
return true;
}
});
}
false

}

// 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
Expand Down Expand Up @@ -223,7 +268,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
});
let index_ty = self.fcx.resolve_vars_if_possible(index_ty);

if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize {
if self.is_builtin_index(&e, *base_ty, index_ty, base.span, index.span)
&& index_ty == self.fcx.tcx.types.usize
Copy link
Member

Choose a reason for hiding this comment

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

You can move this into the is_builtin_index function too I think

{
// Remove the method call record
typeck_results.type_dependent_defs_mut().remove(e.hir_id);
typeck_results.node_substs_mut().remove(e.hir_id);
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/typeck/issue-91633.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-pass
fn f<T> (it: &[T])
where
[T] : std::ops::Index<usize>,
{
let _ = &it[0];
}
fn main(){}