|
1 |
| -use rustc_attr_data_structures::{AttributeKind, OptimizeAttr}; |
| 1 | +use rustc_attr_data_structures::{AttributeKind, OptimizeAttr, UsedBy}; |
2 | 2 | use rustc_feature::{AttributeTemplate, template};
|
3 | 3 | use rustc_session::parse::feature_err;
|
4 | 4 | use rustc_span::{Span, Symbol, sym};
|
@@ -228,3 +228,84 @@ impl<S: Stage> SingleAttributeParser<S> for NoMangleParser {
|
228 | 228 | Some(AttributeKind::NoMangle(cx.attr_span))
|
229 | 229 | }
|
230 | 230 | }
|
| 231 | + |
| 232 | +#[derive(Default)] |
| 233 | +pub(crate) struct UsedParser { |
| 234 | + first_compiler: Option<Span>, |
| 235 | + first_linker: Option<Span>, |
| 236 | +} |
| 237 | + |
| 238 | +// A custom `AttributeParser` is used rather than a Simple attribute parser because |
| 239 | +// - Specifying two `#[used]` attributes is a warning (but will be an error in the future) |
| 240 | +// - But specifying two conflicting attributes: `#[used(compiler)]` and `#[used(linker)]` is already an error today |
| 241 | +// We can change this to a Simple parser once the warning becomes an error |
| 242 | +impl<S: Stage> AttributeParser<S> for UsedParser { |
| 243 | + const ATTRIBUTES: AcceptMapping<Self, S> = &[( |
| 244 | + &[sym::used], |
| 245 | + template!(Word, List: "compiler|linker"), |
| 246 | + |group: &mut Self, cx, args| { |
| 247 | + let used_by = match args { |
| 248 | + ArgParser::NoArgs => UsedBy::Linker, |
| 249 | + ArgParser::List(list) => { |
| 250 | + let Some(l) = list.single() else { |
| 251 | + cx.expected_single_argument(list.span); |
| 252 | + return; |
| 253 | + }; |
| 254 | + |
| 255 | + match l.meta_item().and_then(|i| i.path().word_sym()) { |
| 256 | + Some(sym::compiler) => { |
| 257 | + if !cx.features().used_with_arg() { |
| 258 | + feature_err( |
| 259 | + &cx.sess(), |
| 260 | + sym::used_with_arg, |
| 261 | + cx.attr_span, |
| 262 | + "`#[used(compiler)]` is currently unstable", |
| 263 | + ) |
| 264 | + .emit(); |
| 265 | + } |
| 266 | + UsedBy::Compiler |
| 267 | + } |
| 268 | + Some(sym::linker) => { |
| 269 | + if !cx.features().used_with_arg() { |
| 270 | + feature_err( |
| 271 | + &cx.sess(), |
| 272 | + sym::used_with_arg, |
| 273 | + cx.attr_span, |
| 274 | + "`#[used(linker)]` is currently unstable", |
| 275 | + ) |
| 276 | + .emit(); |
| 277 | + } |
| 278 | + UsedBy::Linker |
| 279 | + } |
| 280 | + _ => { |
| 281 | + cx.expected_specific_argument(l.span(), vec!["compiler", "linker"]); |
| 282 | + return; |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + ArgParser::NameValue(_) => return, |
| 287 | + }; |
| 288 | + |
| 289 | + let target = match used_by { |
| 290 | + UsedBy::Compiler => &mut group.first_compiler, |
| 291 | + UsedBy::Linker => &mut group.first_linker, |
| 292 | + }; |
| 293 | + |
| 294 | + let attr_span = cx.attr_span; |
| 295 | + if let Some(prev) = *target { |
| 296 | + cx.warn_unused_duplicate(prev, attr_span); |
| 297 | + } else { |
| 298 | + *target = Some(attr_span); |
| 299 | + } |
| 300 | + }, |
| 301 | + )]; |
| 302 | + |
| 303 | + fn finalize(self, _cx: &FinalizeContext<'_, '_, S>) -> Option<AttributeKind> { |
| 304 | + // Ratcheting behaviour, if both `linker` and `compiler` are specified, use `linker` |
| 305 | + Some(match (self.first_compiler, self.first_linker) { |
| 306 | + (_, Some(span)) => AttributeKind::Used { used_by: UsedBy::Linker, span }, |
| 307 | + (Some(span), _) => AttributeKind::Used { used_by: UsedBy::Compiler, span }, |
| 308 | + (None, None) => return None, |
| 309 | + }) |
| 310 | + } |
| 311 | +} |
0 commit comments