Skip to content

Commit 154726c

Browse files
authored
Rollup merge of #62243 - petrochenkov:macrodoc, r=eddyb
Improve documentation for built-in macros This is the `libcore` part of #62086. Right now the only effect is improved documentation. The changes in the last few commits are required to make the `libcore` change compile successfully.
2 parents 296e825 + 3274507 commit 154726c

22 files changed

+811
-176
lines changed

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#![feature(const_fn)]
7676
#![feature(const_fn_union)]
7777
#![feature(custom_inner_attributes)]
78+
#![feature(decl_macro)]
7879
#![feature(doc_cfg)]
7980
#![feature(doc_spotlight)]
8081
#![feature(extern_types)]

src/libcore/macros.rs

+628-91
Large diffs are not rendered by default.

src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4010,7 +4010,7 @@ impl<'a> LoweringContext<'a> {
40104010
let attrs = self.lower_attrs(&i.attrs);
40114011
if let ItemKind::MacroDef(ref def) = i.node {
40124012
if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) ||
4013-
attr::contains_name(&i.attrs, sym::rustc_doc_only_macro) {
4013+
attr::contains_name(&i.attrs, sym::rustc_builtin_macro) {
40144014
let body = self.lower_token_stream(def.stream());
40154015
let hir_id = self.lower_node_id(i.id);
40164016
self.exported_macros.push(hir::MacroDef {

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
}

src/librustc_resolve/build_reduced_graph.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -770,27 +770,33 @@ impl<'a> Resolver<'a> {
770770
}
771771

772772
pub fn get_macro(&mut self, res: Res) -> Lrc<SyntaxExtension> {
773+
self.opt_get_macro(res).expect("expected `DefKind::Macro` or `Res::NonMacroAttr`")
774+
}
775+
776+
crate fn opt_get_macro(&mut self, res: Res) -> Option<Lrc<SyntaxExtension>> {
773777
let def_id = match res {
778+
Res::Def(DefKind::Macro(MacroKind::ProcMacroStub), _) =>
779+
return Some(self.non_macro_attr(true)), // some dummy extension
774780
Res::Def(DefKind::Macro(..), def_id) => def_id,
775781
Res::NonMacroAttr(attr_kind) =>
776-
return self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool),
777-
_ => panic!("expected `DefKind::Macro` or `Res::NonMacroAttr`"),
782+
return Some(self.non_macro_attr(attr_kind == NonMacroAttrKind::Tool)),
783+
_ => return None,
778784
};
779785
if let Some(ext) = self.macro_map.get(&def_id) {
780-
return ext.clone();
786+
return Some(ext.clone());
781787
}
782788

783789
let macro_def = match self.cstore.load_macro_untracked(def_id, &self.session) {
784790
LoadedMacro::MacroDef(macro_def) => macro_def,
785-
LoadedMacro::ProcMacro(ext) => return ext,
791+
LoadedMacro::ProcMacro(ext) => return Some(ext),
786792
};
787793

788794
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
789795
&self.session.features_untracked(),
790796
&macro_def,
791797
self.cstore.crate_edition_untracked(def_id.krate)));
792798
self.macro_map.insert(def_id, ext.clone());
793-
ext
799+
Some(ext)
794800
}
795801

796802
/// Ensures that the reduced graph rooted at the given external module

src/librustc_resolve/macros.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,19 @@ impl<'a> Resolver<'a> {
11061106
});
11071107
}
11081108

1109+
crate fn check_reserved_macro_name(&mut self, ident: Ident, res: Res) {
1110+
// Reserve some names that are not quite covered by the general check
1111+
// performed on `Resolver::builtin_attrs`.
1112+
if ident.name == sym::cfg || ident.name == sym::cfg_attr || ident.name == sym::derive {
1113+
let macro_kind = self.opt_get_macro(res).map(|ext| ext.macro_kind());
1114+
if macro_kind.is_some() && sub_namespace_match(macro_kind, Some(MacroKind::Attr)) {
1115+
self.session.span_err(
1116+
ident.span, &format!("name `{}` is reserved in attribute namespace", ident)
1117+
);
1118+
}
1119+
}
1120+
}
1121+
11091122
pub fn define_macro(&mut self,
11101123
item: &ast::Item,
11111124
expansion: Mark,
@@ -1117,13 +1130,14 @@ impl<'a> Resolver<'a> {
11171130
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
11181131
&self.session.features_untracked(),
11191132
item, self.session.edition()));
1133+
let macro_kind = ext.macro_kind();
1134+
let res = Res::Def(DefKind::Macro(macro_kind), def_id);
11201135
self.macro_map.insert(def_id, ext);
11211136

11221137
let def = match item.node { ast::ItemKind::MacroDef(ref def) => def, _ => unreachable!() };
11231138
if def.legacy {
11241139
let ident = ident.modern();
11251140
self.macro_names.insert(ident);
1126-
let res = Res::Def(DefKind::Macro(MacroKind::Bang), def_id);
11271141
let is_macro_export = attr::contains_name(&item.attrs, sym::macro_export);
11281142
let vis = if is_macro_export {
11291143
ty::Visibility::Public
@@ -1142,14 +1156,11 @@ impl<'a> Resolver<'a> {
11421156
self.define(module, ident, MacroNS,
11431157
(res, vis, item.span, expansion, IsMacroExport));
11441158
} else {
1145-
if !attr::contains_name(&item.attrs, sym::rustc_doc_only_macro) {
1146-
self.check_reserved_macro_name(ident, MacroNS);
1147-
}
1159+
self.check_reserved_macro_name(ident, res);
11481160
self.unused_macros.insert(def_id);
11491161
}
11501162
} else {
11511163
let module = self.current_module;
1152-
let res = Res::Def(DefKind::Macro(MacroKind::Bang), def_id);
11531164
let vis = self.resolve_visibility(&item.vis);
11541165
if vis != ty::Visibility::Public {
11551166
self.unused_macros.insert(def_id);

src/librustc_resolve/resolve_imports.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc::{bug, span_bug};
2929
use syntax::ast::{self, Ident, Name, NodeId, CRATE_NODE_ID};
3030
use syntax::ext::base::Determinacy::{self, Determined, Undetermined};
3131
use syntax::ext::hygiene::Mark;
32-
use syntax::symbol::{kw, sym};
32+
use syntax::symbol::kw;
3333
use syntax::util::lev_distance::find_best_match_for_name;
3434
use syntax::{struct_span_err, unwrap_or};
3535
use syntax_pos::{MultiSpan, Span};
@@ -492,35 +492,25 @@ impl<'a> Resolver<'a> {
492492
})
493493
}
494494

495-
crate fn check_reserved_macro_name(&self, ident: Ident, ns: Namespace) {
496-
// Reserve some names that are not quite covered by the general check
497-
// performed on `Resolver::builtin_attrs`.
498-
if ns == MacroNS &&
499-
(ident.name == sym::cfg || ident.name == sym::cfg_attr ||
500-
ident.name == sym::derive) {
501-
self.session.span_err(ident.span,
502-
&format!("name `{}` is reserved in macro namespace", ident));
503-
}
504-
}
505-
506495
// Define the name or return the existing binding if there is a collision.
507496
pub fn try_define(&mut self,
508497
module: Module<'a>,
509498
ident: Ident,
510499
ns: Namespace,
511500
binding: &'a NameBinding<'a>)
512501
-> Result<(), &'a NameBinding<'a>> {
513-
self.check_reserved_macro_name(ident, ns);
502+
let res = binding.res();
503+
self.check_reserved_macro_name(ident, res);
514504
self.set_binding_parent_module(binding, module);
515505
self.update_resolution(module, ident, ns, |this, resolution| {
516506
if let Some(old_binding) = resolution.binding {
517-
if binding.res() == Res::Err {
507+
if res == Res::Err {
518508
// Do not override real bindings with `Res::Err`s from error recovery.
519509
return Ok(());
520510
}
521511
match (old_binding.is_glob_import(), binding.is_glob_import()) {
522512
(true, true) => {
523-
if binding.res() != old_binding.res() {
513+
if res != old_binding.res() {
524514
resolution.binding = Some(this.ambiguity(AmbiguityKind::GlobVsGlob,
525515
old_binding, binding));
526516
} else if !old_binding.vis.is_at_least(binding.vis, &*this) {

src/libstd/macros.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ mod builtin {
410410
///
411411
/// [`panic!`]: ../std/macro.panic.html
412412
#[stable(feature = "compile_error_macro", since = "1.20.0")]
413-
#[rustc_doc_only_macro]
413+
#[rustc_builtin_macro]
414414
macro_rules! compile_error {
415415
($msg:expr) => ({ /* compiler built-in */ });
416416
($msg:expr,) => ({ /* compiler built-in */ });
@@ -462,7 +462,7 @@ mod builtin {
462462
/// assert_eq!(s, format!("hello {}", "world"));
463463
/// ```
464464
#[stable(feature = "rust1", since = "1.0.0")]
465-
#[rustc_doc_only_macro]
465+
#[rustc_builtin_macro]
466466
macro_rules! format_args {
467467
($fmt:expr) => ({ /* compiler built-in */ });
468468
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ });
@@ -500,7 +500,7 @@ mod builtin {
500500
/// error: what's that?!
501501
/// ```
502502
#[stable(feature = "rust1", since = "1.0.0")]
503-
#[rustc_doc_only_macro]
503+
#[rustc_builtin_macro]
504504
macro_rules! env {
505505
($name:expr) => ({ /* compiler built-in */ });
506506
($name:expr,) => ({ /* compiler built-in */ });
@@ -526,7 +526,7 @@ mod builtin {
526526
/// println!("the secret key might be: {:?}", key);
527527
/// ```
528528
#[stable(feature = "rust1", since = "1.0.0")]
529-
#[rustc_doc_only_macro]
529+
#[rustc_builtin_macro]
530530
macro_rules! option_env {
531531
($name:expr) => ({ /* compiler built-in */ });
532532
($name:expr,) => ({ /* compiler built-in */ });
@@ -557,7 +557,7 @@ mod builtin {
557557
/// # }
558558
/// ```
559559
#[unstable(feature = "concat_idents_macro", issue = "29599")]
560-
#[rustc_doc_only_macro]
560+
#[rustc_builtin_macro]
561561
macro_rules! concat_idents {
562562
($($e:ident),+) => ({ /* compiler built-in */ });
563563
($($e:ident,)+) => ({ /* compiler built-in */ });
@@ -579,7 +579,7 @@ mod builtin {
579579
/// assert_eq!(s, "test10btrue");
580580
/// ```
581581
#[stable(feature = "rust1", since = "1.0.0")]
582-
#[rustc_doc_only_macro]
582+
#[rustc_builtin_macro]
583583
macro_rules! concat {
584584
($($e:expr),*) => ({ /* compiler built-in */ });
585585
($($e:expr,)*) => ({ /* compiler built-in */ });
@@ -607,7 +607,7 @@ mod builtin {
607607
/// println!("defined on line: {}", current_line);
608608
/// ```
609609
#[stable(feature = "rust1", since = "1.0.0")]
610-
#[rustc_doc_only_macro]
610+
#[rustc_builtin_macro]
611611
macro_rules! line { () => ({ /* compiler built-in */ }) }
612612

613613
/// Expands to the column number at which it was invoked.
@@ -632,7 +632,7 @@ mod builtin {
632632
/// println!("defined on column: {}", current_col);
633633
/// ```
634634
#[stable(feature = "rust1", since = "1.0.0")]
635-
#[rustc_doc_only_macro]
635+
#[rustc_builtin_macro]
636636
macro_rules! column { () => ({ /* compiler built-in */ }) }
637637

638638
/// Expands to the file name in which it was invoked.
@@ -656,7 +656,7 @@ mod builtin {
656656
/// println!("defined in file: {}", this_file);
657657
/// ```
658658
#[stable(feature = "rust1", since = "1.0.0")]
659-
#[rustc_doc_only_macro]
659+
#[rustc_builtin_macro]
660660
macro_rules! file { () => ({ /* compiler built-in */ }) }
661661

662662
/// Stringifies its arguments.
@@ -675,7 +675,7 @@ mod builtin {
675675
/// assert_eq!(one_plus_one, "1 + 1");
676676
/// ```
677677
#[stable(feature = "rust1", since = "1.0.0")]
678-
#[rustc_doc_only_macro]
678+
#[rustc_builtin_macro]
679679
macro_rules! stringify { ($($t:tt)*) => ({ /* compiler built-in */ }) }
680680

681681
/// Includes a utf8-encoded file as a string.
@@ -709,7 +709,7 @@ mod builtin {
709709
///
710710
/// Compiling 'main.rs' and running the resulting binary will print "adiós".
711711
#[stable(feature = "rust1", since = "1.0.0")]
712-
#[rustc_doc_only_macro]
712+
#[rustc_builtin_macro]
713713
macro_rules! include_str {
714714
($file:expr) => ({ /* compiler built-in */ });
715715
($file:expr,) => ({ /* compiler built-in */ });
@@ -746,7 +746,7 @@ mod builtin {
746746
///
747747
/// Compiling 'main.rs' and running the resulting binary will print "adiós".
748748
#[stable(feature = "rust1", since = "1.0.0")]
749-
#[rustc_doc_only_macro]
749+
#[rustc_builtin_macro]
750750
macro_rules! include_bytes {
751751
($file:expr) => ({ /* compiler built-in */ });
752752
($file:expr,) => ({ /* compiler built-in */ });
@@ -770,7 +770,7 @@ mod builtin {
770770
/// test::foo();
771771
/// ```
772772
#[stable(feature = "rust1", since = "1.0.0")]
773-
#[rustc_doc_only_macro]
773+
#[rustc_builtin_macro]
774774
macro_rules! module_path { () => ({ /* compiler built-in */ }) }
775775

776776
/// Evaluates boolean combinations of configuration flags at compile-time.
@@ -794,7 +794,7 @@ mod builtin {
794794
/// };
795795
/// ```
796796
#[stable(feature = "rust1", since = "1.0.0")]
797-
#[rustc_doc_only_macro]
797+
#[rustc_builtin_macro]
798798
macro_rules! cfg { ($($cfg:tt)*) => ({ /* compiler built-in */ }) }
799799

800800
/// Parses a file as an expression or an item according to the context.
@@ -837,7 +837,7 @@ mod builtin {
837837
/// Compiling 'main.rs' and running the resulting binary will print
838838
/// "🙈🙊🙉🙈🙊🙉".
839839
#[stable(feature = "rust1", since = "1.0.0")]
840-
#[rustc_doc_only_macro]
840+
#[rustc_builtin_macro]
841841
macro_rules! include {
842842
($file:expr) => ({ /* compiler built-in */ });
843843
($file:expr,) => ({ /* compiler built-in */ });
@@ -889,7 +889,7 @@ mod builtin {
889889
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
890890
/// ```
891891
#[stable(feature = "rust1", since = "1.0.0")]
892-
#[rustc_doc_only_macro]
892+
#[rustc_builtin_macro]
893893
macro_rules! assert {
894894
($cond:expr) => ({ /* compiler built-in */ });
895895
($cond:expr,) => ({ /* compiler built-in */ });

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+
}

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::*;

0 commit comments

Comments
 (0)