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 4 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
38 changes: 36 additions & 2 deletions compiler/rustc_typeck/src/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ 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};
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable};
use rustc_middle::ty::{self, ClosureSizeProfileData, Ty, TyCtxt};
use rustc_middle::ty::{self, ClosureSizeProfileData, Ty, TyCtxt, Uint};
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 @@ -223,7 +226,38 @@ 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 {
let mut lang_index = false;

let resolved_base_ty = self.resolve(*base_ty, &base.span);
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
let resolved_index_ty = self.resolve(index_ty, &index.span);
// (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`.
self.tcx().infer_ctxt().enter(|infcx| {
let mut selection_context = SelectionContext::new(&infcx);

let def_id = self.tcx().require_lang_item(LangItem::Index, Some(e.span));
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(),
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
self.fcx.param_env,
binder,
);
let results = selection_context.select(&obligation);

if let Ok(Some(ImplSource::Param(..))) = results && matches!(index_ty.kind(), Uint(ty::UintTy::Usize)){
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
lang_index = true;
}

});

if base_ty.builtin_index().is_some()
ouz-a marked this conversation as resolved.
Show resolved Hide resolved
&& 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

&& !lang_index
{
// 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(){}