Skip to content

Commit d9aaca7

Browse files
committed
store typeck lints in the TypeckTables
Otherwise they are a "hidden output"
1 parent 65b93eb commit d9aaca7

File tree

13 files changed

+87
-32
lines changed

13 files changed

+87
-32
lines changed

src/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/lint/context.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use lint::{Level, LevelSource, Lint, LintId, LintPass, LintSource};
3333
use lint::{EarlyLintPassObject, LateLintPassObject};
3434
use lint::{Default, CommandLine, Node, Allow, Warn, Deny, Forbid};
3535
use lint::builtin;
36+
use rustc_serialize::{Decoder, Decodable, Encoder, Encodable};
3637
use util::nodemap::FxHashMap;
3738

3839
use std::cmp;
@@ -82,7 +83,7 @@ pub struct LintStore {
8283

8384
/// When you call `add_lint` on the session, you wind up storing one
8485
/// of these, which records a "potential lint" at a particular point.
85-
#[derive(PartialEq)]
86+
#[derive(PartialEq, RustcEncodable, RustcDecodable)]
8687
pub struct EarlyLint {
8788
/// what lint is this? (e.g., `dead_code`)
8889
pub id: LintId,
@@ -558,7 +559,7 @@ pub trait LintContext<'tcx>: Sized {
558559
self.lookup_and_emit(lint, Some(span), msg);
559560
}
560561

561-
fn early_lint(&self, early_lint: EarlyLint) {
562+
fn early_lint(&self, early_lint: &EarlyLint) {
562563
let span = early_lint.diagnostic.span.primary_span().expect("early lint w/o primary span");
563564
let mut err = self.struct_span_lint(early_lint.id.lint,
564565
span,
@@ -774,7 +775,7 @@ impl<'a, 'tcx> hir_visit::Visitor<'tcx> for LateContext<'a, 'tcx> {
774775
// Output any lints that were previously added to the session.
775776
fn visit_id(&mut self, id: ast::NodeId) {
776777
let lints = self.sess().lints.borrow_mut().take(id);
777-
for early_lint in lints {
778+
for early_lint in lints.iter().chain(self.tables.lints.get(id)) {
778779
debug!("LateContext::visit_id: id={:?} early_lint={:?}", id, early_lint);
779780
self.early_lint(early_lint);
780781
}
@@ -1251,7 +1252,7 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12511252
// Lints may be assigned to the whole crate.
12521253
let lints = cx.sess.lints.borrow_mut().take(ast::CRATE_NODE_ID);
12531254
for early_lint in lints {
1254-
cx.early_lint(early_lint);
1255+
cx.early_lint(&early_lint);
12551256
}
12561257

12571258
// since the root module isn't visited as an item (because it isn't an
@@ -1274,3 +1275,22 @@ pub fn check_ast_crate(sess: &Session, krate: &ast::Crate) {
12741275
}
12751276
}
12761277
}
1278+
1279+
impl Encodable for LintId {
1280+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1281+
s.emit_str(&self.lint.name.to_lowercase())
1282+
}
1283+
}
1284+
1285+
impl Decodable for LintId {
1286+
#[inline]
1287+
fn decode<D: Decoder>(d: &mut D) -> Result<LintId, D::Error> {
1288+
let s = d.read_str()?;
1289+
ty::tls::with(|tcx| {
1290+
match tcx.sess.lint_store.borrow().find_lint(&s, tcx.sess, None) {
1291+
Ok(id) => Ok(id),
1292+
Err(_) => panic!("invalid lint-id `{}`", s),
1293+
}
1294+
})
1295+
}
1296+
}

src/librustc/lint/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
pub use self::Level::*;
3232
pub use self::LintSource::*;
3333

34+
use hir;
35+
use hir::intravisit::FnKind;
3436
use std::hash;
3537
use std::ascii::AsciiExt;
3638
use syntax_pos::Span;
37-
use hir::intravisit::FnKind;
3839
use syntax::visit as ast_visit;
3940
use syntax::ast;
40-
use hir;
4141

4242
pub use lint::context::{LateContext, EarlyContext, LintContext, LintStore,
4343
raw_emit_lint, check_crate, check_ast_crate, gather_attrs,

src/librustc/lint/table.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
use syntax::ast;
212
use syntax_pos::MultiSpan;
313
use util::nodemap::NodeMap;
414

515
use super::{Lint, LintId, EarlyLint, IntoEarlyLint};
616

17+
#[derive(RustcEncodable, RustcDecodable)]
718
pub struct LintTable {
819
map: NodeMap<Vec<EarlyLint>>
920
}
@@ -44,6 +55,10 @@ impl LintTable {
4455
self.map.remove(&id).unwrap_or(vec![])
4556
}
4657

58+
pub fn transfer(&mut self, into: &mut LintTable) {
59+
into.map.extend(self.map.drain());
60+
}
61+
4762
/// Returns the first (id, lint) pair that is non-empty. Used to
4863
/// implement a sanity check in lints that all node-ids are
4964
/// visited.

src/librustc/ty/context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
1313
use dep_graph::{DepGraph, DepTrackingMap};
1414
use session::Session;
15+
use lint;
1516
use middle;
1617
use hir::TraitMap;
1718
use hir::def::Def;
@@ -237,6 +238,9 @@ pub struct TypeckTables<'tcx> {
237238
/// Maps a cast expression to its kind. This is keyed on the
238239
/// *from* expression of the cast, not the cast itself.
239240
pub cast_kinds: NodeMap<ty::cast::CastKind>,
241+
242+
/// Lints for the body of this fn generated by typeck.
243+
pub lints: lint::LintTable,
240244
}
241245

242246
impl<'tcx> TypeckTables<'tcx> {
@@ -253,6 +257,7 @@ impl<'tcx> TypeckTables<'tcx> {
253257
liberated_fn_sigs: NodeMap(),
254258
fru_field_types: NodeMap(),
255259
cast_kinds: NodeMap(),
260+
lints: lint::LintTable::new(),
256261
}
257262
}
258263

src/librustc_errors/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ path = "lib.rs"
99
crate-type = ["dylib"]
1010

1111
[dependencies]
12+
serialize = { path = "../libserialize" }
1213
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_errors/diagnostic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax_pos::{MultiSpan, Span};
1717
use snippet::Style;
1818

1919
#[must_use]
20-
#[derive(Clone, Debug, PartialEq)]
20+
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
2121
pub struct Diagnostic {
2222
pub level: Level,
2323
pub message: Vec<(String, Style)>,
@@ -27,7 +27,7 @@ pub struct Diagnostic {
2727
}
2828

2929
/// For example a note attached to an error.
30-
#[derive(Clone, Debug, PartialEq)]
30+
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
3131
pub struct SubDiagnostic {
3232
pub level: Level,
3333
pub message: Vec<(String, Style)>,

src/librustc_errors/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
extern crate term;
2828
extern crate libc;
29+
extern crate serialize as rustc_serialize;
2930
extern crate syntax_pos;
3031

3132
pub use emitter::ColorConfig;
@@ -49,7 +50,7 @@ mod lock;
4950
use syntax_pos::{BytePos, Loc, FileLinesResult, FileName, MultiSpan, Span, NO_EXPANSION};
5051
use syntax_pos::MacroBacktrace;
5152

52-
#[derive(Clone, Debug, PartialEq)]
53+
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
5354
pub enum RenderSpan {
5455
/// A FullSpan renders with both with an initial line for the
5556
/// message, prefixed by file:linenum, followed by a summary of
@@ -63,7 +64,7 @@ pub enum RenderSpan {
6364
Suggestion(CodeSuggestion),
6465
}
6566

66-
#[derive(Clone, Debug, PartialEq)]
67+
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
6768
pub struct CodeSuggestion {
6869
pub msp: MultiSpan,
6970
pub substitutes: Vec<String>,
@@ -477,7 +478,7 @@ impl Handler {
477478
}
478479

479480

480-
#[derive(Copy, PartialEq, Clone, Debug)]
481+
#[derive(Copy, PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
481482
pub enum Level {
482483
Bug,
483484
Fatal,

src/librustc_errors/snippet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub struct StyledString {
204204
pub style: Style,
205205
}
206206

207-
#[derive(Copy, Clone, Debug, PartialEq)]
207+
#[derive(Copy, Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
208208
pub enum Style {
209209
HeaderMsg,
210210
FileNameStyle,

src/librustc_typeck/check/cast.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,25 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
311311
let t_cast = self.cast_ty;
312312
let t_expr = self.expr_ty;
313313
if t_cast.is_numeric() && t_expr.is_numeric() {
314-
fcx.tcx.sess.add_lint(lint::builtin::TRIVIAL_NUMERIC_CASTS,
315-
self.expr.id,
316-
self.span,
317-
format!("trivial numeric cast: `{}` as `{}`. Cast can be \
318-
replaced by coercion, this might require type \
319-
ascription or a temporary variable",
320-
fcx.ty_to_string(t_expr),
321-
fcx.ty_to_string(t_cast)));
314+
fcx.tables.borrow_mut().lints.add_lint(
315+
lint::builtin::TRIVIAL_NUMERIC_CASTS,
316+
self.expr.id,
317+
self.span,
318+
format!("trivial numeric cast: `{}` as `{}`. Cast can be \
319+
replaced by coercion, this might require type \
320+
ascription or a temporary variable",
321+
fcx.ty_to_string(t_expr),
322+
fcx.ty_to_string(t_cast)));
322323
} else {
323-
fcx.tcx.sess.add_lint(lint::builtin::TRIVIAL_CASTS,
324-
self.expr.id,
325-
self.span,
326-
format!("trivial cast: `{}` as `{}`. Cast can be \
327-
replaced by coercion, this might require type \
328-
ascription or a temporary variable",
329-
fcx.ty_to_string(t_expr),
330-
fcx.ty_to_string(t_cast)));
324+
fcx.tables.borrow_mut().lints.add_lint(
325+
lint::builtin::TRIVIAL_CASTS,
326+
self.expr.id,
327+
self.span,
328+
format!("trivial cast: `{}` as `{}`. Cast can be \
329+
replaced by coercion, this might require type \
330+
ascription or a temporary variable",
331+
fcx.ty_to_string(t_expr),
332+
fcx.ty_to_string(t_cast)));
331333
}
332334

333335
}

src/librustc_typeck/check/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,9 +1521,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
15211521
if self.diverges.get() == Diverges::Always {
15221522
self.diverges.set(Diverges::WarnedAlways);
15231523

1524-
self.tcx.sess.add_lint(lint::builtin::UNREACHABLE_CODE,
1525-
id, span,
1526-
format!("unreachable {}", kind));
1524+
self.tables.borrow_mut().lints.add_lint(
1525+
lint::builtin::UNREACHABLE_CODE,
1526+
id, span,
1527+
format!("unreachable {}", kind));
15271528
}
15281529
}
15291530

src/librustc_typeck/check/writeback.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5252
wbcx.visit_deferred_obligations(item_id);
5353
wbcx.visit_type_nodes();
5454
wbcx.visit_cast_types();
55+
wbcx.visit_lints();
5556

5657
let tables = self.tcx.alloc_tables(wbcx.tables);
5758
self.tcx.tables.borrow_mut().insert(item_def_id, tables);
@@ -301,6 +302,14 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
301302
self.fcx.tables.borrow().cast_kinds.iter().map(|(&key, &value)| (key, value)));
302303
}
303304

305+
fn visit_lints(&mut self) {
306+
if self.fcx.writeback_errors.get() {
307+
return
308+
}
309+
310+
self.fcx.tables.borrow_mut().lints.transfer(&mut self.tables.lints);
311+
}
312+
304313
fn visit_anon_types(&self) {
305314
if self.fcx.writeback_errors.get() {
306315
return

src/libsyntax_pos/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct Span {
6666
/// the error, and would be rendered with `^^^`.
6767
/// - they can have a *label*. In this case, the label is written next
6868
/// to the mark in the snippet when we render.
69-
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
69+
#[derive(Clone, Debug, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
7070
pub struct MultiSpan {
7171
primary_spans: Vec<Span>,
7272
span_labels: Vec<(Span, String)>,

0 commit comments

Comments
 (0)