Skip to content

Commit f993107

Browse files
committed
privacy: Only opaque macros leak private things
1 parent 7e6b011 commit f993107

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

Diff for: src/librustc_privacy/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_data_structures::fx::FxHashSet;
2828
use syntax::ast::Ident;
2929
use syntax::attr;
3030
use syntax::symbol::{kw, sym};
31+
use syntax_pos::hygiene::Transparency;
3132
use syntax_pos::Span;
3233

3334
use std::{cmp, fmt, mem};
@@ -743,7 +744,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
743744
}
744745

745746
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef) {
746-
if md.legacy {
747+
if attr::find_transparency(&md.attrs, md.legacy).0 != Transparency::Opaque {
747748
self.update(md.hir_id, Some(AccessLevel::Public));
748749
return
749750
}

Diff for: src/libsyntax/attr/builtin.rs

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::feature_gate::{Features, GatedCfg};
55
use crate::parse::ParseSess;
66

77
use errors::{Applicability, Handler};
8+
use syntax_pos::hygiene::Transparency;
89
use syntax_pos::{symbol::Symbol, symbol::sym, Span};
910

1011
use super::{mark_used, MetaItemKind};
@@ -854,3 +855,35 @@ fn int_type_of_word(s: Symbol) -> Option<IntType> {
854855
_ => None
855856
}
856857
}
858+
859+
pub enum TransparencyError {
860+
UnknownTransparency(Symbol, Span),
861+
MultipleTransparencyAttrs(Span, Span),
862+
}
863+
864+
pub fn find_transparency(
865+
attrs: &[Attribute], is_legacy: bool
866+
) -> (Transparency, Option<TransparencyError>) {
867+
let mut transparency = None;
868+
let mut error = None;
869+
for attr in attrs {
870+
if attr.check_name(sym::rustc_macro_transparency) {
871+
if let Some((_, old_span)) = transparency {
872+
error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span));
873+
break;
874+
} else if let Some(value) = attr.value_str() {
875+
transparency = Some((match &*value.as_str() {
876+
"transparent" => Transparency::Transparent,
877+
"semitransparent" => Transparency::SemiTransparent,
878+
"opaque" => Transparency::Opaque,
879+
_ => {
880+
error = Some(TransparencyError::UnknownTransparency(value, attr.span));
881+
continue;
882+
}
883+
}, attr.span));
884+
}
885+
}
886+
}
887+
let fallback = if is_legacy { Transparency::SemiTransparent } else { Transparency::Opaque };
888+
(transparency.map_or(fallback, |t| t.0), error)
889+
}

Diff for: src/libsyntax/attr/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
33
mod builtin;
44

5-
pub use builtin::{
6-
cfg_matches, contains_feature_attr, eval_condition, find_crate_name, find_deprecation,
7-
find_repr_attrs, find_stability, find_unwind_attr, Deprecation, InlineAttr, OptimizeAttr,
8-
IntType, ReprAttr, RustcDeprecation, Stability, StabilityLevel, UnwindAttr,
9-
};
5+
pub use builtin::*;
106
pub use IntType::*;
117
pub use ReprAttr::*;
128
pub use StabilityLevel::*;

Diff for: src/libsyntax/ext/tt/macro_rules.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::edition::Edition;
22
use crate::ext::base::{DummyResult, ExtCtxt, MacResult, TTMacroExpander};
33
use crate::ext::base::{SyntaxExtension, SyntaxExtensionKind};
44
use crate::ext::expand::{AstFragment, AstFragmentKind};
5-
use crate::ext::hygiene::Transparency;
65
use crate::ext::tt::macro_parser::{parse, parse_failure_msg};
76
use crate::ext::tt::macro_parser::{Error, Failure, Success};
87
use crate::ext::tt::macro_parser::{MatchedNonterminal, MatchedSeq};
@@ -15,7 +14,7 @@ use crate::parse::token::{self, NtTT, Token};
1514
use crate::parse::{Directory, ParseSess};
1615
use crate::symbol::{kw, sym, Symbol};
1716
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
18-
use crate::{ast, attr};
17+
use crate::{ast, attr, attr::TransparencyError};
1918

2019
use errors::FatalError;
2120
use log::debug;
@@ -380,17 +379,19 @@ pub fn compile(
380379
let expander: Box<_> =
381380
Box::new(MacroRulesMacroExpander { name: def.ident, lhses, rhses, valid });
382381

383-
let value_str = attr::first_attr_value_str_by_name(&def.attrs, sym::rustc_macro_transparency);
384-
let default_transparency = value_str.and_then(|s| Some(match &*s.as_str() {
385-
"transparent" => Transparency::Transparent,
386-
"semitransparent" => Transparency::SemiTransparent,
387-
"opaque" => Transparency::Opaque,
388-
_ => {
389-
let msg = format!("unknown macro transparency: `{}`", s);
390-
sess.span_diagnostic.span_err(def.span, &msg);
391-
return None;
392-
}
393-
})).unwrap_or(if body.legacy { Transparency::SemiTransparent } else { Transparency::Opaque });
382+
let (default_transparency, transparency_error) =
383+
attr::find_transparency(&def.attrs, body.legacy);
384+
match transparency_error {
385+
Some(TransparencyError::UnknownTransparency(value, span)) =>
386+
sess.span_diagnostic.span_err(
387+
span, &format!("unknown macro transparency: `{}`", value)
388+
),
389+
Some(TransparencyError::MultipleTransparencyAttrs(old_span, new_span)) =>
390+
sess.span_diagnostic.span_err(
391+
vec![old_span, new_span], "multiple macro transparency attributes"
392+
),
393+
None => {}
394+
}
394395

395396
let allow_internal_unstable =
396397
attr::find_by_name(&def.attrs, sym::allow_internal_unstable).map(|attr| {

0 commit comments

Comments
 (0)