Skip to content

Commit 8c7500f

Browse files
committed
add lint levels to VisibilityScope
1 parent acb73db commit 8c7500f

File tree

24 files changed

+269
-53
lines changed

24 files changed

+269
-53
lines changed

src/librustc/ich/impls_mir.rs

+18
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl_stable_hash_for!(struct mir::LocalDecl<'tcx> {
2828
name,
2929
source_info,
3030
internal,
31+
lexical_scope,
3132
is_user_variable
3233
});
3334
impl_stable_hash_for!(struct mir::UpvarDecl { debug_name, by_ref });
@@ -75,6 +76,22 @@ for mir::Terminator<'gcx> {
7576
}
7677
}
7778

79+
impl<'gcx, T> HashStable<StableHashingContext<'gcx>> for mir::ClearOnDecode<T>
80+
where T: HashStable<StableHashingContext<'gcx>>
81+
{
82+
#[inline]
83+
fn hash_stable<W: StableHasherResult>(&self,
84+
hcx: &mut StableHashingContext<'gcx>,
85+
hasher: &mut StableHasher<W>) {
86+
mem::discriminant(self).hash_stable(hcx, hasher);
87+
match *self {
88+
mir::ClearOnDecode::Clear => {}
89+
mir::ClearOnDecode::Set(ref value) => {
90+
value.hash_stable(hcx, hasher);
91+
}
92+
}
93+
}
94+
}
7895

7996
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Local {
8097
#[inline]
@@ -347,6 +364,7 @@ for mir::ProjectionElem<'gcx, V, T>
347364
}
348365

349366
impl_stable_hash_for!(struct mir::VisibilityScopeData { span, parent_scope });
367+
impl_stable_hash_for!(struct mir::VisibilityScopeInfo { lint_root });
350368

351369
impl<'gcx> HashStable<StableHashingContext<'gcx>> for mir::Operand<'gcx> {
352370
fn hash_stable<W: StableHasherResult>(&self,

src/librustc/lint/levels.rs

+5
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ impl LintLevelMap {
384384
self.sets.get_lint_level(lint, *idx, None)
385385
})
386386
}
387+
388+
/// Returns if this `id` has lint level information.
389+
pub fn lint_level_set(&self, id: HirId) -> Option<u32> {
390+
self.id_to_set.get(&id).cloned()
391+
}
387392
}
388393

389394
impl<'gcx> HashStable<StableHashingContext<'gcx>> for LintLevelMap {

src/librustc/mir/mod.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1818
use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
1919
use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
2020
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
21+
use rustc_serialize as serialize;
2122
use hir::def::CtorKind;
2223
use hir::def_id::DefId;
2324
use ty::subst::{Subst, Substs};
@@ -33,7 +34,7 @@ use std::fmt::{self, Debug, Formatter, Write};
3334
use std::{iter, u32};
3435
use std::ops::{Index, IndexMut};
3536
use std::vec::IntoIter;
36-
use syntax::ast::Name;
37+
use syntax::ast::{self, Name};
3738
use syntax_pos::Span;
3839

3940
mod cache;
@@ -96,6 +97,10 @@ pub struct Mir<'tcx> {
9697
/// and used (eventually) for debuginfo. Indexed by a `VisibilityScope`.
9798
pub visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
9899

100+
/// Crate-local information for each visibility scope, that can't (and
101+
/// needn't) be tracked across crates.
102+
pub visibility_scope_info: ClearOnDecode<IndexVec<VisibilityScope, VisibilityScopeInfo>>,
103+
99104
/// Rvalues promoted from this function, such as borrows of constants.
100105
/// Each of them is the Mir of a constant with the fn's type parameters
101106
/// in scope, but a separate set of locals.
@@ -151,6 +156,8 @@ pub const START_BLOCK: BasicBlock = BasicBlock(0);
151156
impl<'tcx> Mir<'tcx> {
152157
pub fn new(basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
153158
visibility_scopes: IndexVec<VisibilityScope, VisibilityScopeData>,
159+
visibility_scope_info: ClearOnDecode<IndexVec<VisibilityScope,
160+
VisibilityScopeInfo>>,
154161
promoted: IndexVec<Promoted, Mir<'tcx>>,
155162
return_ty: Ty<'tcx>,
156163
yield_ty: Option<Ty<'tcx>>,
@@ -167,6 +174,7 @@ impl<'tcx> Mir<'tcx> {
167174
Mir {
168175
basic_blocks,
169176
visibility_scopes,
177+
visibility_scope_info,
170178
promoted,
171179
return_ty,
172180
yield_ty,
@@ -278,9 +286,16 @@ impl<'tcx> Mir<'tcx> {
278286
}
279287
}
280288

289+
#[derive(Clone, Debug)]
290+
pub struct VisibilityScopeInfo {
291+
/// A NodeId with lint levels equivalent to this scope's lint levels.
292+
pub lint_root: ast::NodeId,
293+
}
294+
281295
impl_stable_hash_for!(struct Mir<'tcx> {
282296
basic_blocks,
283297
visibility_scopes,
298+
visibility_scope_info,
284299
promoted,
285300
return_ty,
286301
yield_ty,
@@ -310,6 +325,24 @@ impl<'tcx> IndexMut<BasicBlock> for Mir<'tcx> {
310325
}
311326
}
312327

328+
#[derive(Clone, Debug)]
329+
pub enum ClearOnDecode<T> {
330+
Clear,
331+
Set(T)
332+
}
333+
334+
impl<T> serialize::Encodable for ClearOnDecode<T> {
335+
fn encode<S: serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
336+
serialize::Encodable::encode(&(), s)
337+
}
338+
}
339+
340+
impl<T> serialize::Decodable for ClearOnDecode<T> {
341+
fn decode<D: serialize::Decoder>(d: &mut D) -> Result<Self, D::Error> {
342+
serialize::Decodable::decode(d).map(|()| ClearOnDecode::Clear)
343+
}
344+
}
345+
313346
/// Grouped information about the source code origin of a MIR entity.
314347
/// Intended to be inspected by diagnostics and debuginfo.
315348
/// Most passes can work with it as a whole, within a single function.
@@ -438,6 +471,12 @@ pub struct LocalDecl<'tcx> {
438471

439472
/// Source info of the local.
440473
pub source_info: SourceInfo,
474+
475+
/// The *lexical* visibility scope the local is defined
476+
/// in. If the local was defined in a let-statement, this
477+
/// is *within* the let-statement, rather than outside
478+
/// of iit.
479+
pub lexical_scope: VisibilityScope,
441480
}
442481

443482
impl<'tcx> LocalDecl<'tcx> {
@@ -452,6 +491,7 @@ impl<'tcx> LocalDecl<'tcx> {
452491
span,
453492
scope: ARGUMENT_VISIBILITY_SCOPE
454493
},
494+
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
455495
internal: false,
456496
is_user_variable: false
457497
}
@@ -468,6 +508,7 @@ impl<'tcx> LocalDecl<'tcx> {
468508
span,
469509
scope: ARGUMENT_VISIBILITY_SCOPE
470510
},
511+
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
471512
internal: true,
472513
is_user_variable: false
473514
}
@@ -485,6 +526,7 @@ impl<'tcx> LocalDecl<'tcx> {
485526
span,
486527
scope: ARGUMENT_VISIBILITY_SCOPE
487528
},
529+
lexical_scope: ARGUMENT_VISIBILITY_SCOPE,
488530
internal: false,
489531
name: None, // FIXME maybe we do want some name here?
490532
is_user_variable: false
@@ -1607,6 +1649,7 @@ impl<'tcx> TypeFoldable<'tcx> for Mir<'tcx> {
16071649
Mir {
16081650
basic_blocks: self.basic_blocks.fold_with(folder),
16091651
visibility_scopes: self.visibility_scopes.clone(),
1652+
visibility_scope_info: self.visibility_scope_info.clone(),
16101653
promoted: self.promoted.fold_with(folder),
16111654
return_ty: self.return_ty.fold_with(folder),
16121655
yield_ty: self.yield_ty.fold_with(folder),

src/librustc/mir/visit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -690,11 +690,13 @@ macro_rules! make_mir_visitor {
690690
name: _,
691691
ref $($mutability)* source_info,
692692
internal: _,
693+
ref $($mutability)* lexical_scope,
693694
is_user_variable: _,
694695
} = *local_decl;
695696

696697
self.visit_ty(ty, Lookup::Src(*source_info));
697698
self.visit_source_info(source_info);
699+
self.visit_visibility_scope(lexical_scope);
698700
}
699701

700702
fn super_visibility_scope(&mut self,

src/librustc_driver/driver.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1009,15 +1009,16 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
10091009
// FIXME: ariel points SimplifyBranches should run after
10101010
// mir-borrowck; otherwise code within `if false { ... }` would
10111011
// not be checked.
1012-
passes.push_pass(MIR_VALIDATED,
1013-
mir::transform::simplify_branches::SimplifyBranches::new("initial"));
10141012
passes.push_pass(MIR_VALIDATED, mir::transform::simplify::SimplifyCfg::new("qualify-consts"));
10151013
passes.push_pass(MIR_VALIDATED, mir::transform::nll::NLL);
10161014

10171015
// borrowck runs between MIR_VALIDATED and MIR_OPTIMIZED.
10181016

1019-
// These next passes must be executed together
10201017
passes.push_pass(MIR_OPTIMIZED, mir::transform::no_landing_pads::NoLandingPads);
1018+
passes.push_pass(MIR_OPTIMIZED,
1019+
mir::transform::simplify_branches::SimplifyBranches::new("initial"));
1020+
1021+
// These next passes must be executed together
10211022
passes.push_pass(MIR_OPTIMIZED, mir::transform::add_call_guards::CriticalCallEdges);
10221023
passes.push_pass(MIR_OPTIMIZED, mir::transform::elaborate_drops::ElaborateDrops);
10231024
passes.push_pass(MIR_OPTIMIZED, mir::transform::no_landing_pads::NoLandingPads);

src/librustc_mir/build/block.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2424
let Block { region_scope, opt_destruction_scope, span, stmts, expr, targeted_by_break } =
2525
self.hir.mirror(ast_block);
2626
self.in_opt_scope(opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
27-
this.in_scope((region_scope, source_info), block, move |this| {
27+
this.in_scope((region_scope, source_info), LintLevel::Inherited, block, move |this| {
2828
if targeted_by_break {
2929
// This is a `break`-able block (currently only `catch { ... }`)
3030
let exit_block = this.cfg.start_new_block();
@@ -76,27 +76,35 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
7676
StmtKind::Expr { scope, expr } => {
7777
unpack!(block = this.in_opt_scope(
7878
opt_destruction_scope.map(|de|(de, source_info)), block, |this| {
79-
this.in_scope((scope, source_info), block, |this| {
79+
let si = (scope, source_info);
80+
this.in_scope(si, LintLevel::Inherited, block, |this| {
8081
let expr = this.hir.mirror(expr);
8182
this.stmt_expr(block, expr)
8283
})
8384
}));
8485
}
85-
StmtKind::Let { remainder_scope, init_scope, pattern, initializer } => {
86+
StmtKind::Let {
87+
remainder_scope,
88+
init_scope,
89+
pattern,
90+
initializer,
91+
lint_level
92+
} => {
8693
// Enter the remainder scope, i.e. the bindings' destruction scope.
8794
this.push_scope((remainder_scope, source_info));
8895
let_scope_stack.push(remainder_scope);
8996

9097
// Declare the bindings, which may create a visibility scope.
9198
let remainder_span = remainder_scope.span(this.hir.tcx(),
9299
&this.hir.region_scope_tree);
93-
let scope = this.declare_bindings(None, remainder_span, &pattern);
100+
let scope = this.declare_bindings(None, remainder_span, lint_level, &pattern);
94101

95102
// Evaluate the initializer, if present.
96103
if let Some(init) = initializer {
97104
unpack!(block = this.in_opt_scope(
98105
opt_destruction_scope.map(|de|(de, source_info)), block, move |this| {
99-
this.in_scope((init_scope, source_info), block, move |this| {
106+
let scope = (init_scope, source_info);
107+
this.in_scope(scope, lint_level, block, move |this| {
100108
// FIXME #30046 ^~~~
101109
this.expr_into_pattern(block, pattern, init)
102110
})

src/librustc_mir/build/expr/as_constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2929
let Expr { ty, temp_lifetime: _, span, kind }
3030
= expr;
3131
match kind {
32-
ExprKind::Scope { region_scope: _, value } =>
32+
ExprKind::Scope { region_scope: _, lint_level: _, value } =>
3333
this.as_constant(value),
3434
ExprKind::Literal { literal } =>
3535
Constant { span: span, ty: ty, literal: literal },

src/librustc_mir/build/expr/as_lvalue.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3939
let expr_span = expr.span;
4040
let source_info = this.source_info(expr_span);
4141
match expr.kind {
42-
ExprKind::Scope { region_scope, value } => {
43-
this.in_scope((region_scope, source_info), block, |this| {
42+
ExprKind::Scope { region_scope, lint_level, value } => {
43+
this.in_scope((region_scope, source_info), lint_level, block, |this| {
4444
this.as_lvalue(block, value)
4545
})
4646
}

src/librustc_mir/build/expr/as_operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
5555
debug!("expr_as_operand(block={:?}, expr={:?})", block, expr);
5656
let this = self;
5757

58-
if let ExprKind::Scope { region_scope, value } = expr.kind {
58+
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
5959
let source_info = this.source_info(expr.span);
6060
let region_scope = (region_scope, source_info);
61-
return this.in_scope(region_scope, block, |this| {
61+
return this.in_scope(region_scope, lint_level, block, |this| {
6262
this.as_operand(block, scope, value)
6363
});
6464
}

src/librustc_mir/build/expr/as_rvalue.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
5858
let source_info = this.source_info(expr_span);
5959

6060
match expr.kind {
61-
ExprKind::Scope { region_scope, value } => {
61+
ExprKind::Scope { region_scope, lint_level, value } => {
6262
let region_scope = (region_scope, source_info);
63-
this.in_scope(region_scope, block, |this| this.as_rvalue(block, scope, value))
63+
this.in_scope(region_scope, lint_level, block,
64+
|this| this.as_rvalue(block, scope, value))
6465
}
6566
ExprKind::Repeat { value, count } => {
6667
let value_operand = unpack!(block = this.as_operand(block, scope, value));

src/librustc_mir/build/expr/as_temp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
4141

4242
let expr_span = expr.span;
4343
let source_info = this.source_info(expr_span);
44-
if let ExprKind::Scope { region_scope, value } = expr.kind {
45-
return this.in_scope((region_scope, source_info), block, |this| {
44+
if let ExprKind::Scope { region_scope, lint_level, value } = expr.kind {
45+
return this.in_scope((region_scope, source_info), lint_level, block, |this| {
4646
this.as_temp(block, temp_lifetime, value)
4747
});
4848
}

src/librustc_mir/build/expr/into.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
3838
let source_info = this.source_info(expr_span);
3939

4040
match expr.kind {
41-
ExprKind::Scope { region_scope, value } => {
41+
ExprKind::Scope { region_scope, lint_level, value } => {
4242
let region_scope = (region_scope, source_info);
43-
this.in_scope(region_scope, block, |this| this.into(destination, block, value))
43+
this.in_scope(region_scope, lint_level, block,
44+
|this| this.into(destination, block, value))
4445
}
4546
ExprKind::Block { body: ast_block } => {
4647
this.ast_block(destination, block, ast_block, source_info)

src/librustc_mir/build/expr/stmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
2222
// Handle a number of expressions that don't need a destination at all. This
2323
// avoids needing a mountain of temporary `()` variables.
2424
match expr.kind {
25-
ExprKind::Scope { region_scope, value } => {
25+
ExprKind::Scope { region_scope, lint_level, value } => {
2626
let value = this.hir.mirror(value);
27-
this.in_scope((region_scope, source_info), block, |this| {
27+
this.in_scope((region_scope, source_info), lint_level, block, |this| {
2828
this.stmt_expr(block, value)
2929
})
3030
}

0 commit comments

Comments
 (0)