Skip to content

Commit a745614

Browse files
committed
Use lint instead of warning
1 parent 1a9239c commit a745614

File tree

8 files changed

+128
-31
lines changed

8 files changed

+128
-31
lines changed

src/librustc/lint/builtin.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ declare_lint! {
117117
Allow,
118118
"detects trivial casts of numeric types which could be removed"
119119
}
120+
121+
declare_lint! {
122+
pub PRIVATE_IN_PUBLIC,
123+
Warn,
124+
"detect private items in public interfaces not caught by the old implementation"
125+
}
126+
120127
/// Does nothing as a lint pass, but registers some `Lint`s
121128
/// which are used by other parts of the compiler.
122129
#[derive(Copy, Clone)]
@@ -141,6 +148,7 @@ impl LintPass for HardwiredLints {
141148
FAT_PTR_TRANSMUTES,
142149
TRIVIAL_CASTS,
143150
TRIVIAL_NUMERIC_CASTS,
151+
PRIVATE_IN_PUBLIC,
144152
CONST_ERR
145153
)
146154
}

src/librustc_lint/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
146146
UNUSED_MUT, UNREACHABLE_CODE, UNUSED_MUST_USE,
147147
UNUSED_UNSAFE, PATH_STATEMENTS, UNUSED_ATTRIBUTES);
148148

149+
add_lint_group!(sess, "future_incompatible",
150+
PRIVATE_IN_PUBLIC);
151+
149152
// We have one lint pass defined specially
150153
store.register_late_pass(sess, false, box lint::GatherNodeLevels);
151154

src/librustc_privacy/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use std::mem::replace;
3838
use rustc_front::hir;
3939
use rustc_front::intravisit::{self, Visitor};
4040

41+
use rustc::lint;
4142
use rustc::middle::def;
4243
use rustc::middle::def_id::DefId;
4344
use rustc::middle::privacy::{AccessLevel, AccessLevels};
@@ -1488,9 +1489,17 @@ impl<'a, 'tcx: 'a, 'v> Visitor<'v> for SearchInterfaceForPrivateItemsVisitor<'a,
14881489
}
14891490
if item.vis != hir::Public {
14901491
if !self.is_quiet {
1491-
let is_warning = !self.old_error_set.contains(&ty.id);
1492-
span_err_or_warn!(is_warning, self.tcx.sess, ty.span, E0446,
1493-
"private type in public interface");
1492+
if self.old_error_set.contains(&ty.id) {
1493+
span_err!(self.tcx.sess, ty.span, E0446,
1494+
"private type in public interface");
1495+
} else {
1496+
self.tcx.sess.add_lint (
1497+
lint::builtin::PRIVATE_IN_PUBLIC,
1498+
node_id,
1499+
ty.span,
1500+
"private type in public interface".to_string()
1501+
);
1502+
}
14941503
}
14951504
self.is_public = false;
14961505
}
@@ -1515,9 +1524,15 @@ impl<'a, 'tcx: 'a, 'v> Visitor<'v> for SearchInterfaceForPrivateItemsVisitor<'a,
15151524
if let Some(ast_map::NodeItem(ref item)) = self.tcx.map.find(node_id) {
15161525
if item.vis != hir::Public {
15171526
if !self.is_quiet {
1518-
let is_warning = !self.old_error_set.contains(&trait_ref.ref_id);
1519-
span_err_or_warn!(is_warning, self.tcx.sess, trait_ref.path.span, E0445,
1520-
"private trait in public interface");
1527+
if self.old_error_set.contains(&trait_ref.ref_id) {
1528+
span_err!(self.tcx.sess, trait_ref.path.span, E0445,
1529+
"private trait in public interface");
1530+
} else {
1531+
self.tcx.sess.add_lint(lint::builtin::PRIVATE_IN_PUBLIC,
1532+
node_id,
1533+
trait_ref.path.span,
1534+
"private trait in public interface".to_string());
1535+
}
15211536
}
15221537
self.is_public = false;
15231538
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 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+
11+
// Checks for private types in public interfaces
12+
13+
type Foo = u8;
14+
pub fn foo(f: Foo) {} //~ ERROR private type in public interface
15+
16+
fn main() {}

src/test/compile-fail/issue-28450.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// Checks for private types in public interfaces
1212

13+
#![feature(rustc_attrs)]
14+
1315
struct Priv;
1416

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

31-
type Foo = u8;
32-
pub fn foo(f: Foo) {} //~ ERROR private type in public interface
33-
3433
pub trait Exporter {
3534
type Output;
3635
}
@@ -49,6 +48,7 @@ pub fn block() -> <Helper as Exporter>::Output {
4948
Inner
5049
}
5150

52-
fn main() {
51+
#[rustc_error]
52+
fn main() { //~ ERROR compilation successful
5353
block().poke();
5454
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2014 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+
11+
mod m1 {
12+
#![deny(private_in_public)]
13+
14+
pub struct Pub;
15+
struct Priv;
16+
17+
impl Pub {
18+
pub fn f() -> Priv {} //~ ERROR private type in public interface
19+
}
20+
}
21+
22+
mod m2 {
23+
#![deny(future_incompatible)]
24+
25+
pub struct Pub;
26+
struct Priv;
27+
28+
impl Pub {
29+
pub fn f() -> Priv {} //~ ERROR private type in public interface
30+
}
31+
}
32+
33+
fn main() {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2014 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+
11+
#![feature(rustc_attrs)]
12+
#![allow(dead_code)]
13+
14+
use std::marker;
15+
16+
struct Private<T>(marker::PhantomData<T>);
17+
pub struct Public<T>(marker::PhantomData<T>);
18+
19+
pub trait PubTrait {
20+
type Output;
21+
}
22+
23+
type PrivAlias = Public<i8>;
24+
25+
trait PrivTrait2 {
26+
type Alias;
27+
}
28+
impl PrivTrait2 for Private<isize> {
29+
type Alias = Public<u8>;
30+
}
31+
32+
impl PubTrait for PrivAlias {
33+
type Output = Private<isize>; //~ WARN private type in public interface
34+
}
35+
36+
impl PubTrait for <Private<isize> as PrivTrait2>::Alias {
37+
type Output = Private<isize>; //~ WARN private type in public interface
38+
}
39+
40+
#[rustc_error]
41+
fn main() {} //~ ERROR compilation successful

src/test/compile-fail/lint-visible-private-types.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ pub trait PubTrait {
7575
}
7676

7777
impl PubTrait for Public<isize> {
78-
fn bar(&self) -> Private<isize> { panic!() } //~ WARN private type in public interface
79-
fn baz() -> Private<isize> { panic!() } //~ WARN private type in public interface
78+
fn bar(&self) -> Private<isize> { panic!() } // Warns in lint checking phase
79+
fn baz() -> Private<isize> { panic!() } // Warns in lint checking phase
8080
}
8181
impl PubTrait for Public<Private<isize>> {
8282
fn bar(&self) -> Private<isize> { panic!() }
@@ -121,22 +121,3 @@ impl<T: ParamTrait<Private<isize>>> //~ ERROR private type in public interface
121121
ParamTrait<T> for Public<i8> {
122122
fn foo() -> T { panic!() }
123123
}
124-
125-
type PrivAlias = Public<i8>;
126-
127-
trait PrivTrait2 {
128-
type Alias;
129-
}
130-
impl PrivTrait2 for Private<isize> {
131-
type Alias = Public<u8>;
132-
}
133-
134-
impl PubTrait for PrivAlias {
135-
fn bar(&self) -> Private<isize> { panic!() } //~ WARN private type in public interface
136-
fn baz() -> Private<isize> { panic!() } //~ WARN private type in public interface
137-
}
138-
139-
impl PubTrait for <Private<isize> as PrivTrait2>::Alias {
140-
fn bar(&self) -> Private<isize> { panic!() } //~ WARN private type in public interface
141-
fn baz() -> Private<isize> { panic!() } //~ WARN private type in public interface
142-
}

0 commit comments

Comments
 (0)