rustc_attr_parsing/attributes/
transparency.rs

1use rustc_ast::attr::AttributeExt;
2use rustc_attr_data_structures::TransparencyError;
3use rustc_span::hygiene::Transparency;
4use rustc_span::sym;
5
6pub fn find_transparency(
7    attrs: &[impl AttributeExt],
8    macro_rules: bool,
9) -> (Transparency, Option<TransparencyError>) {
10    let mut transparency = None;
11    let mut error = None;
12    for attr in attrs {
13        if attr.has_name(sym::rustc_macro_transparency) {
14            if let Some((_, old_span)) = transparency {
15                error = Some(TransparencyError::MultipleTransparencyAttrs(old_span, attr.span()));
16                break;
17            } else if let Some(value) = attr.value_str() {
18                transparency = Some((
19                    match value {
20                        sym::transparent => Transparency::Transparent,
21                        sym::semitransparent => Transparency::SemiTransparent,
22                        sym::opaque => Transparency::Opaque,
23                        _ => {
24                            error =
25                                Some(TransparencyError::UnknownTransparency(value, attr.span()));
26                            continue;
27                        }
28                    },
29                    attr.span(),
30                ));
31            }
32        }
33    }
34    let fallback = if macro_rules { Transparency::SemiTransparent } else { Transparency::Opaque };
35    (transparency.map_or(fallback, |t| t.0), error)
36}