privacy: Only opaque macros leak private things

This commit is contained in:
Vadim Petrochenkov 2019-06-30 01:12:04 +03:00
parent 987be89db3
commit 920a17a60c
4 changed files with 50 additions and 19 deletions

View File

@ -28,6 +28,7 @@ use rustc_data_structures::fx::FxHashSet;
use syntax::ast::Ident;
use syntax::attr;
use syntax::symbol::{kw, sym};
use syntax_pos::hygiene::Transparency;
use syntax_pos::Span;
use std::{cmp, fmt, mem};
@ -743,7 +744,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
}
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
if md.legacy {
if attr::find_transparency(&md.attrs, md.legacy).0 != Transparency::Opaque {
self.update(md.hir_id, Some(AccessLevel::Public));
return
}

View File

@ -5,6 +5,7 @@ use crate::feature_gate::{Features, GatedCfg};
use crate::parse::ParseSess;
use errors::{Applicability, Handler};
use syntax_pos::hygiene::Transparency;
use syntax_pos::{symbol::Symbol, symbol::sym, Span};
use super::{mark_used, MetaItemKind};
@ -854,3 +855,35 @@ fn int_type_of_word(s: Symbol) -> Option<IntType> {
_ => None
}
}
pub enum TransparencyError {
UnknownTransparency(Symbol, Span),
MultipleTransparencyAttrs(Span, Span),
}
pub fn find_transparency(
attrs: &[Attribute], is_legacy: bool
) -> (Transparency, Option<TransparencyError>) {
let mut transparency = None;
let mut error = None;
for attr in attrs {
if attr.check_name(sym::rustc_macro_transparency) {
if let Some((_, old_span)) = transparency {
error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span));
break;
} else if let Some(value) = attr.value_str() {
transparency = Some((match &*value.as_str() {
"transparent" => Transparency::Transparent,
"semitransparent" => Transparency::SemiTransparent,
"opaque" => Transparency::Opaque,
_ => {
error = Some(TransparencyError::UnknownTransparency(value, attr.span));
continue;
}
}, attr.span));
}
}
}
let fallback = if is_legacy { Transparency::SemiTransparent } else { Transparency::Opaque };
(transparency.map_or(fallback, |t| t.0), error)
}

View File

@ -2,11 +2,7 @@
mod builtin;
pub use builtin::{
cfg_matches, contains_feature_attr, eval_condition, find_crate_name, find_deprecation,
find_repr_attrs, find_stability, find_unwind_attr, Deprecation, InlineAttr, OptimizeAttr,
IntType, ReprAttr, RustcDeprecation, Stability, StabilityLevel, UnwindAttr,
};
pub use builtin::*;
pub use IntType::*;
pub use ReprAttr::*;
pub use StabilityLevel::*;

View File

@ -2,7 +2,6 @@ use crate::edition::Edition;
use crate::ext::base::{DummyResult, ExtCtxt, MacResult, TTMacroExpander};
use crate::ext::base::{SyntaxExtension, SyntaxExtensionKind};
use crate::ext::expand::{AstFragment, AstFragmentKind};
use crate::ext::hygiene::Transparency;
use crate::ext::tt::macro_parser::{parse, parse_failure_msg};
use crate::ext::tt::macro_parser::{Error, Failure, Success};
use crate::ext::tt::macro_parser::{MatchedNonterminal, MatchedSeq};
@ -15,7 +14,7 @@ use crate::parse::token::{self, NtTT, Token};
use crate::parse::{Directory, ParseSess};
use crate::symbol::{kw, sym, Symbol};
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
use crate::{ast, attr};
use crate::{ast, attr, attr::TransparencyError};
use errors::FatalError;
use log::debug;
@ -380,17 +379,19 @@ pub fn compile(
let expander: Box<_> =
Box::new(MacroRulesMacroExpander { name: def.ident, lhses, rhses, valid });
let value_str = attr::first_attr_value_str_by_name(&def.attrs, sym::rustc_macro_transparency);
let default_transparency = value_str.and_then(|s| Some(match &*s.as_str() {
"transparent" => Transparency::Transparent,
"semitransparent" => Transparency::SemiTransparent,
"opaque" => Transparency::Opaque,
_ => {
let msg = format!("unknown macro transparency: `{}`", s);
sess.span_diagnostic.span_err(def.span, &msg);
return None;
}
})).unwrap_or(if body.legacy { Transparency::SemiTransparent } else { Transparency::Opaque });
let (default_transparency, transparency_error) =
attr::find_transparency(&def.attrs, body.legacy);
match transparency_error {
Some(TransparencyError::UnknownTransparency(value, span)) =>
sess.span_diagnostic.span_err(
span, &format!("unknown macro transparency: `{}`", value)
),
Some(TransparencyError::MultipleTransparencyAttrs(old_span, new_span)) =>
sess.span_diagnostic.span_err(
vec![old_span, new_span], "multiple macro transparency attributes"
),
None => {}
}
let allow_internal_unstable =
attr::find_by_name(&def.attrs, sym::allow_internal_unstable).map(|attr| {