Skip to content

Commit

Permalink
Use lint instead of warning
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Dec 18, 2015
1 parent 1a9239c commit a745614
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 31 deletions.
8 changes: 8 additions & 0 deletions src/librustc/lint/builtin.rs
Expand Up @@ -117,6 +117,13 @@ declare_lint! {
Allow,
"detects trivial casts of numeric types which could be removed"
}

declare_lint! {
pub PRIVATE_IN_PUBLIC,
Warn,
"detect private items in public interfaces not caught by the old implementation"
}

/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
#[derive(Copy, Clone)]
Expand All @@ -141,6 +148,7 @@ impl LintPass for HardwiredLints {
FAT_PTR_TRANSMUTES,
TRIVIAL_CASTS,
TRIVIAL_NUMERIC_CASTS,
PRIVATE_IN_PUBLIC,
CONST_ERR
)
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_lint/lib.rs
Expand Up @@ -146,6 +146,9 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
UNUSED_MUT, UNREACHABLE_CODE, UNUSED_MUST_USE,
UNUSED_UNSAFE, PATH_STATEMENTS, UNUSED_ATTRIBUTES);

add_lint_group!(sess, "future_incompatible",
PRIVATE_IN_PUBLIC);

// We have one lint pass defined specially
store.register_late_pass(sess, false, box lint::GatherNodeLevels);

Expand Down
27 changes: 21 additions & 6 deletions src/librustc_privacy/lib.rs
Expand Up @@ -38,6 +38,7 @@ use std::mem::replace;
use rustc_front::hir;
use rustc_front::intravisit::{self, Visitor};

use rustc::lint;
use rustc::middle::def;
use rustc::middle::def_id::DefId;
use rustc::middle::privacy::{AccessLevel, AccessLevels};
Expand Down Expand Up @@ -1488,9 +1489,17 @@ impl<'a, 'tcx: 'a, 'v> Visitor<'v> for SearchInterfaceForPrivateItemsVisitor<'a,
}
if item.vis != hir::Public {
if !self.is_quiet {
let is_warning = !self.old_error_set.contains(&ty.id);
span_err_or_warn!(is_warning, self.tcx.sess, ty.span, E0446,
"private type in public interface");
if self.old_error_set.contains(&ty.id) {
span_err!(self.tcx.sess, ty.span, E0446,
"private type in public interface");
} else {
self.tcx.sess.add_lint (
lint::builtin::PRIVATE_IN_PUBLIC,
node_id,
ty.span,
"private type in public interface".to_string()
);
}
}
self.is_public = false;
}
Expand All @@ -1515,9 +1524,15 @@ impl<'a, 'tcx: 'a, 'v> Visitor<'v> for SearchInterfaceForPrivateItemsVisitor<'a,
if let Some(ast_map::NodeItem(ref item)) = self.tcx.map.find(node_id) {
if item.vis != hir::Public {
if !self.is_quiet {
let is_warning = !self.old_error_set.contains(&trait_ref.ref_id);
span_err_or_warn!(is_warning, self.tcx.sess, trait_ref.path.span, E0445,
"private trait in public interface");
if self.old_error_set.contains(&trait_ref.ref_id) {
span_err!(self.tcx.sess, trait_ref.path.span, E0445,
"private trait in public interface");
} else {
self.tcx.sess.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
node_id,
trait_ref.path.span,
"private trait in public interface".to_string());
}
}
self.is_public = false;
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/issue-28450-1.rs
@@ -0,0 +1,16 @@
// Copyright 2015 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.

// Checks for private types in public interfaces

type Foo = u8;
pub fn foo(f: Foo) {} //~ ERROR private type in public interface

fn main() {}
8 changes: 4 additions & 4 deletions src/test/compile-fail/issue-28450.rs
Expand Up @@ -10,6 +10,8 @@

// Checks for private types in public interfaces

#![feature(rustc_attrs)]

struct Priv;

pub use self::private::public;
Expand All @@ -28,9 +30,6 @@ impl<T> Pointer for *const T { type Pointee = T; }
pub type __CFArrayRevealed = <CFArrayRef as Pointer>::Pointee;
//~^ WARN private type in public interface

type Foo = u8;
pub fn foo(f: Foo) {} //~ ERROR private type in public interface

pub trait Exporter {
type Output;
}
Expand All @@ -49,6 +48,7 @@ pub fn block() -> <Helper as Exporter>::Output {
Inner
}

fn main() {
#[rustc_error]
fn main() { //~ ERROR compilation successful
block().poke();
}
33 changes: 33 additions & 0 deletions src/test/compile-fail/lint-private-in-public.rs
@@ -0,0 +1,33 @@
// Copyright 2014 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.

mod m1 {
#![deny(private_in_public)]

pub struct Pub;
struct Priv;

impl Pub {
pub fn f() -> Priv {} //~ ERROR private type in public interface
}
}

mod m2 {
#![deny(future_incompatible)]

pub struct Pub;
struct Priv;

impl Pub {
pub fn f() -> Priv {} //~ ERROR private type in public interface
}
}

fn main() {}
41 changes: 41 additions & 0 deletions src/test/compile-fail/lint-visible-private-types-1.rs
@@ -0,0 +1,41 @@
// Copyright 2014 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.

#![feature(rustc_attrs)]
#![allow(dead_code)]

use std::marker;

struct Private<T>(marker::PhantomData<T>);
pub struct Public<T>(marker::PhantomData<T>);

pub trait PubTrait {
type Output;
}

type PrivAlias = Public<i8>;

trait PrivTrait2 {
type Alias;
}
impl PrivTrait2 for Private<isize> {
type Alias = Public<u8>;
}

impl PubTrait for PrivAlias {
type Output = Private<isize>; //~ WARN private type in public interface
}

impl PubTrait for <Private<isize> as PrivTrait2>::Alias {
type Output = Private<isize>; //~ WARN private type in public interface
}

#[rustc_error]
fn main() {} //~ ERROR compilation successful
23 changes: 2 additions & 21 deletions src/test/compile-fail/lint-visible-private-types.rs
Expand Up @@ -75,8 +75,8 @@ pub trait PubTrait {
}

impl PubTrait for Public<isize> {
fn bar(&self) -> Private<isize> { panic!() } //~ WARN private type in public interface
fn baz() -> Private<isize> { panic!() } //~ WARN private type in public interface
fn bar(&self) -> Private<isize> { panic!() } // Warns in lint checking phase
fn baz() -> Private<isize> { panic!() } // Warns in lint checking phase
}
impl PubTrait for Public<Private<isize>> {
fn bar(&self) -> Private<isize> { panic!() }
Expand Down Expand Up @@ -121,22 +121,3 @@ impl<T: ParamTrait<Private<isize>>> //~ ERROR private type in public interface
ParamTrait<T> for Public<i8> {
fn foo() -> T { panic!() }
}

type PrivAlias = Public<i8>;

trait PrivTrait2 {
type Alias;
}
impl PrivTrait2 for Private<isize> {
type Alias = Public<u8>;
}

impl PubTrait for PrivAlias {
fn bar(&self) -> Private<isize> { panic!() } //~ WARN private type in public interface
fn baz() -> Private<isize> { panic!() } //~ WARN private type in public interface
}

impl PubTrait for <Private<isize> as PrivTrait2>::Alias {
fn bar(&self) -> Private<isize> { panic!() } //~ WARN private type in public interface
fn baz() -> Private<isize> { panic!() } //~ WARN private type in public interface
}

0 comments on commit a745614

Please sign in to comment.