Skip to content

Commit

Permalink
Add warn(unreachable_pub) to rustc_metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Aug 29, 2024
1 parent 4b92682 commit 05e0738
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
24 changes: 18 additions & 6 deletions compiler/rustc_macros/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use quote::{quote, quote_spanned};
use syn::parse_quote;
use syn::spanned::Spanned;

pub(super) fn type_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn type_decodable_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
let decoder_ty = quote! { __D };
let bound = if s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
quote! { <I = ::rustc_middle::ty::TyCtxt<'tcx>> }
Expand All @@ -20,7 +22,9 @@ pub(super) fn type_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_
decodable_body(s, decoder_ty)
}

pub(super) fn meta_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn meta_decodable_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
s.add_impl_generic(parse_quote! { 'tcx });
}
Expand All @@ -41,7 +45,9 @@ pub(super) fn decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro
decodable_body(s, decoder_ty)
}

pub(super) fn decodable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn decodable_generic_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
let decoder_ty = quote! { __D };
s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_serialize::Decoder });
s.add_bounds(synstructure::AddBounds::Generics);
Expand Down Expand Up @@ -123,7 +129,9 @@ fn decode_field(field: &syn::Field) -> proc_macro2::TokenStream {
quote_spanned! { field_span=> #decode_inner_method(#__decoder) }
}

pub(super) fn type_encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn type_encodable_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
let bound = if s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
quote! { <I = ::rustc_middle::ty::TyCtxt<'tcx>> }
} else if s.ast().generics.type_params().any(|ty| ty.ident == "I") {
Expand All @@ -140,7 +148,9 @@ pub(super) fn type_encodable_derive(mut s: synstructure::Structure<'_>) -> proc_
encodable_body(s, encoder_ty, false)
}

pub(super) fn meta_encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn meta_encodable_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") {
s.add_impl_generic(parse_quote! { 'tcx });
}
Expand All @@ -161,7 +171,9 @@ pub(super) fn encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro
encodable_body(s, encoder_ty, false)
}

pub(super) fn encodable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn encodable_generic_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
let encoder_ty = quote! { __E };
s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_serialize::Encoder });
s.add_bounds(synstructure::AddBounds::Generics);
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_macros/src/type_visitable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use quote::quote;
use syn::parse_quote;

pub(super) fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
pub(super) fn type_visitable_derive(
mut s: synstructure::Structure<'_>,
) -> proc_macro2::TokenStream {
if let syn::Data::Union(_) = s.ast().data {
panic!("cannot derive on union")
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#![feature(proc_macro_internals)]
#![feature(rustdoc_internals)]
#![feature(trusted_len)]
#![warn(unreachable_pub)]
// tidy-alphabetical-end

extern crate proc_macro;
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ impl std::ops::Deref for MetadataBlob {

impl MetadataBlob {
/// Runs the [`MemDecoder`] validation and if it passes, constructs a new [`MetadataBlob`].
pub fn new(slice: OwnedSlice) -> Result<Self, ()> {
pub(crate) fn new(slice: OwnedSlice) -> Result<Self, ()> {
if MemDecoder::new(&slice, 0).is_ok() { Ok(Self(slice)) } else { Err(()) }
}

/// Since this has passed the validation of [`MetadataBlob::new`], this returns bytes which are
/// known to pass the [`MemDecoder`] validation.
pub fn bytes(&self) -> &OwnedSlice {
pub(crate) fn bytes(&self) -> &OwnedSlice {
&self.0
}
}
Expand Down Expand Up @@ -332,12 +332,12 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
}

#[inline]
pub fn blob(&self) -> &'a MetadataBlob {
pub(crate) fn blob(&self) -> &'a MetadataBlob {
self.blob
}

#[inline]
pub fn cdata(&self) -> CrateMetadataRef<'a> {
fn cdata(&self) -> CrateMetadataRef<'a> {
debug_assert!(self.cdata.is_some(), "missing CrateMetadata in DecodeContext");
self.cdata.unwrap()
}
Expand Down Expand Up @@ -377,7 +377,7 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
}

#[inline]
pub fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
self.opaque.read_raw_bytes(len)
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameterized_over_tcx! {

impl DefPathHashMapRef<'_> {
#[inline]
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
pub(crate) fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
match *self {
DefPathHashMapRef::OwnedFromMetadata(ref map) => {
map.get(&def_path_hash.local_hash()).unwrap()
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2309,7 +2309,7 @@ fn encode_root_position(mut file: &File, pos: usize) -> Result<(), std::io::Erro
Ok(())
}

pub fn provide(providers: &mut Providers) {
pub(crate) fn provide(providers: &mut Providers) {
*providers = Providers {
doc_link_resolutions: |tcx, def_id| {
tcx.resolutions(())
Expand Down

0 comments on commit 05e0738

Please sign in to comment.