From 42e16223cfbe9fc1ee22cde8e698914eaf8dab55 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 9 Sep 2015 11:15:33 +0200 Subject: [PATCH] Add new error code for visibility inside a function --- src/librustc_privacy/diagnostics.rs | 26 +++++++++++++++++++++----- src/librustc_privacy/lib.rs | 3 ++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/librustc_privacy/diagnostics.rs b/src/librustc_privacy/diagnostics.rs index cdd6af2d13d9..2c6931fafdee 100644 --- a/src/librustc_privacy/diagnostics.rs +++ b/src/librustc_privacy/diagnostics.rs @@ -13,7 +13,8 @@ register_long_diagnostics! { E0445: r##" -A private trait was used on a "public" type. Erroneous code example: +A private trait was used on a public type parameter bound. Erroneous code +examples: ``` trait Foo { @@ -23,8 +24,9 @@ trait Foo { pub trait Bar : Foo {} // error: private trait in exported type parameter bound ``` -To solve this error, please ensure the trait is accessible at the same level of -the type(s) on which it's implemented. Example: +To solve this error, please ensure that the trait is also public and accessible +at the same level of the public functions or types which are bound on it. +Example: ``` pub trait Foo { // we set the Foo trait public @@ -48,8 +50,8 @@ mod Foo { } ``` -To solve this error, please ensure the type is accessible at the same level of -the exported type signature. Example: +To solve this error, please ensure that the type is also public and accessible +at the same level of the public functions or types which use it. Example: ``` mod Foo { @@ -62,4 +64,18 @@ mod Foo { ``` "##, +E0447: r##" +The `pub` keyword was used inside a function. Erroneous code example: + +``` +fn foo() { + pub struct Bar; // error: visibility has no effect inside functions +} +``` + +Since we cannot access inside function's elements, the visibility of its +elements does not impact outer code. So using the `pub` keyword in this context +is invalid. +"##, + } \ No newline at end of file diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 7b14ef134eb1..584fe21fae10 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -1098,7 +1098,8 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> { let tcx = self.tcx; fn check_inherited(tcx: &ty::ctxt, sp: Span, vis: hir::Visibility) { if vis != hir::Inherited { - tcx.sess.span_err(sp, "visibility has no effect inside functions"); + span_err!(tcx.sess, sp, E0447, + "visibility has no effect inside functions"); } } let check_struct = |def: &hir::StructDef| {