<commit_before>// 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.

//! This query borrow-checks the MIR to (further) ensure it is not broken.

use rustc::hir::def_id::{DefId};
use rustc::infer::{InferCtxt};
use rustc::ty::{self, TyCtxt, ParamEnv};
use rustc::ty::maps::Providers;
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue};
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
use rustc::mir::transform::{MirSource};

use rustc_data_structures::indexed_set::{self, IdxSetBuf};
use rustc_data_structures::indexed_vec::{Idx};

use syntax::ast::{self};
use syntax_pos::{DUMMY_SP, Span};

use dataflow::{do_dataflow};
use dataflow::{MoveDataParamEnv};
use dataflow::{BitDenotation, BlockSets, DataflowResults, DataflowResultsConsumer};
use dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
use dataflow::{Borrows, BorrowData, BorrowIndex};
use dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex, LookupResult};
use util::borrowck_errors::{BorrowckErrors, Origin};

use self::MutateMode::{JustWrite, WriteAndRead};
use self::ConsumeKind::{Consume};


pub fn provide(providers: &mut Providers) {
    *providers = Providers {
        mir_borrowck,
        ..*providers
    };
}

fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
    let mir = tcx.mir_validated(def_id);
    let src = MirSource::from_local_def_id(tcx, def_id);
    debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));

    let mir: &Mir<'tcx> = &mir.borrow();
    if !tcx.has_attr(def_id, "rustc_mir_borrowck") || !tcx.sess.opts.debugging_opts.borrowck_mir {
        return;
    }

    let id = src.item_id();
    let attributes = tcx.get_attrs(def_id);
    let param_env = tcx.param_env(def_id);
    tcx.infer_ctxt().enter(|_infcx| {

        let move_data = MoveData::gather_moves(mir, tcx, param_env);
        let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };
        let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
        let flow_borrows = do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
                                       Borrows::new(tcx, mir),
                                       |bd, i| bd.location(i));
        let flow_inits = do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
                                     MaybeInitializedLvals::new(tcx, mir, &mdpe),
                                     |bd, i| &bd.move_data().move_paths[i]);
        let flow_uninits = do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
                                       MaybeUninitializedLvals::new(tcx, mir, &mdpe),
                                       |bd, i| &bd.move_data().move_paths[i]);

        let mut mbcx = MirBorrowckCtxt {
            tcx: tcx,
            mir: mir,
            node_id: id,
            move_data: &mdpe.move_data,<commit_msg>Auto merge of #44032 - pnkfelix:mir-borrowck-fix-orisnotand, r=eddyb<commit_after>// 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.

//! This query borrow-checks the MIR to (further) ensure it is not broken.

use rustc::hir::def_id::{DefId};
use rustc::infer::{InferCtxt};
use rustc::ty::{self, TyCtxt, ParamEnv};
use rustc::ty::maps::Providers;
use rustc::mir::{AssertMessage, BasicBlock, BorrowKind, Location, Lvalue};
use rustc::mir::{Mir, Mutability, Operand, Projection, ProjectionElem, Rvalue};
use rustc::mir::{Statement, StatementKind, Terminator, TerminatorKind};
use rustc::mir::transform::{MirSource};

use rustc_data_structures::indexed_set::{self, IdxSetBuf};
use rustc_data_structures::indexed_vec::{Idx};

use syntax::ast::{self};
use syntax_pos::{DUMMY_SP, Span};

use dataflow::{do_dataflow};
use dataflow::{MoveDataParamEnv};
use dataflow::{BitDenotation, BlockSets, DataflowResults, DataflowResultsConsumer};
use dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
use dataflow::{Borrows, BorrowData, BorrowIndex};
use dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex, LookupResult};
use util::borrowck_errors::{BorrowckErrors, Origin};

use self::MutateMode::{JustWrite, WriteAndRead};
use self::ConsumeKind::{Consume};


pub fn provide(providers: &mut Providers) {
    *providers = Providers {
        mir_borrowck,
        ..*providers
    };
}

fn mir_borrowck<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
    let mir = tcx.mir_validated(def_id);
    let src = MirSource::from_local_def_id(tcx, def_id);
    debug!("run query mir_borrowck: {}", tcx.node_path_str(src.item_id()));

    let mir: &Mir<'tcx> = &mir.borrow();
    if !tcx.has_attr(def_id, "rustc_mir_borrowck") && !tcx.sess.opts.debugging_opts.borrowck_mir {
        return;
    }

    let id = src.item_id();
    let attributes = tcx.get_attrs(def_id);
    let param_env = tcx.param_env(def_id);
    tcx.infer_ctxt().enter(|_infcx| {

        let move_data = MoveData::gather_moves(mir, tcx, param_env);
        let mdpe = MoveDataParamEnv { move_data: move_data, param_env: param_env };
        let dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
        let flow_borrows = do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
                                       Borrows::new(tcx, mir),
                                       |bd, i| bd.location(i));
        let flow_inits = do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
                                     MaybeInitializedLvals::new(tcx, mir, &mdpe),
                                     |bd, i| &bd.move_data().move_paths[i]);
        let flow_uninits = do_dataflow(tcx, mir, id, &attributes, &dead_unwinds,
                                       MaybeUninitializedLvals::new(tcx, mir, &mdpe),
                                       |bd, i| &bd.move_data().move_paths[i]);

        let mut mbcx = MirBorrowckCtxt {
            tcx: tcx,
            mir: mir,
            node_id: id,
            move_data: &mdpe.move_data,
{'fraction_non_alphanumeric': 0.13697023023903798, 'fraction_numerical': 0.0030796304443466782, 'mean_word_length': 3.374599101988454, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6323156', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2303, 'n_words': 500}