|
3 | 3 | use rustc_ast::{self as ast, LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit, NodeId};
|
4 | 4 | use rustc_ast_pretty::pprust;
|
5 | 5 | use rustc_attr_data_structures::RustcVersion;
|
| 6 | +use rustc_errors::Applicability; |
6 | 7 | use rustc_feature::{Features, GatedCfg, find_gated_cfg};
|
7 | 8 | use rustc_session::Session;
|
8 | 9 | use rustc_session::config::ExpectedValues;
|
9 | 10 | use rustc_session::lint::BuiltinLintDiag;
|
10 | 11 | use rustc_session::lint::builtin::UNEXPECTED_CFGS;
|
11 | 12 | use rustc_session::parse::feature_err;
|
12 | 13 | use rustc_span::{Span, Symbol, kw, sym};
|
| 14 | +use rustc_target::spec::apple; |
13 | 15 |
|
14 | 16 | use crate::util::UnsupportedLiteralReason;
|
15 | 17 | use crate::{fluent_generated, parse_version, session_diagnostics};
|
@@ -150,6 +152,129 @@ pub fn eval_condition(
|
150 | 152 | RustcVersion::CURRENT >= min_version
|
151 | 153 | }
|
152 | 154 | }
|
| 155 | + ast::MetaItemKind::List(mis) if cfg.name_or_empty() == sym::os_version_min => { |
| 156 | + try_gate_cfg(sym::os_version_min, cfg.span, sess, features); |
| 157 | + |
| 158 | + let (platform, version) = match &mis[..] { |
| 159 | + [platform, version] => (platform, version), |
| 160 | + [..] => { |
| 161 | + dcx.emit_err(session_diagnostics::ExpectedPlatformAndVersionLiterals { |
| 162 | + span: cfg.span, |
| 163 | + }); |
| 164 | + return false; |
| 165 | + } |
| 166 | + }; |
| 167 | + |
| 168 | + let (platform_sym, platform_span) = match platform { |
| 169 | + MetaItemInner::Lit(MetaItemLit { |
| 170 | + kind: LitKind::Str(platform_sym, ..), |
| 171 | + span: platform_span, |
| 172 | + .. |
| 173 | + }) => (platform_sym, platform_span), |
| 174 | + MetaItemInner::Lit(MetaItemLit { span, .. }) |
| 175 | + | MetaItemInner::MetaItem(MetaItem { span, .. }) => { |
| 176 | + dcx.emit_err(session_diagnostics::ExpectedPlatformLiteral { span: *span }); |
| 177 | + return false; |
| 178 | + } |
| 179 | + }; |
| 180 | + |
| 181 | + let (version_sym, version_span) = match version { |
| 182 | + MetaItemInner::Lit(MetaItemLit { |
| 183 | + kind: LitKind::Str(version_sym, ..), |
| 184 | + span: version_span, |
| 185 | + .. |
| 186 | + }) => (version_sym, version_span), |
| 187 | + MetaItemInner::Lit(MetaItemLit { span, .. }) |
| 188 | + | MetaItemInner::MetaItem(MetaItem { span, .. }) => { |
| 189 | + dcx.emit_err(session_diagnostics::ExpectedVersionLiteral { span: *span }); |
| 190 | + return false; |
| 191 | + } |
| 192 | + }; |
| 193 | + |
| 194 | + // Always parse version, regardless of current target platform. |
| 195 | + let version = match *platform_sym { |
| 196 | + // Apple platforms follow the same versioning schema. |
| 197 | + sym::macos | sym::ios | sym::tvos | sym::watchos | sym::visionos => { |
| 198 | + match version_sym.as_str().parse() { |
| 199 | + Ok(version) => { |
| 200 | + let os_min = apple::OSVersion::os_minimum_deployment_target( |
| 201 | + &platform_sym.as_str(), |
| 202 | + ); |
| 203 | + |
| 204 | + // It's unnecessary to specify `cfg_target_os(...)` for a platform |
| 205 | + // version that is lower than the minimum targetted by `rustc` (instead, |
| 206 | + // make the item always available). |
| 207 | + // |
| 208 | + // This is correct _now_, but once we bump versions next time, we should |
| 209 | + // maybe make this a lint so that users can opt-in to supporting older |
| 210 | + // `rustc` versions? Or perhaps only fire the warning when Cargo's |
| 211 | + // `rust-version` field is above the version where the bump happened? Or |
| 212 | + // perhaps keep the version we check against low for a sufficiently long |
| 213 | + // time? |
| 214 | + if version <= os_min { |
| 215 | + sess.dcx() |
| 216 | + .create_warn( |
| 217 | + session_diagnostics::AppleVersionUnnecessarilyLow { |
| 218 | + span: *version_span, |
| 219 | + os_min: os_min.fmt_pretty().to_string(), |
| 220 | + }, |
| 221 | + ) |
| 222 | + .with_span_suggestion( |
| 223 | + cfg.span, |
| 224 | + "use `target_os` instead", |
| 225 | + format!("target_os = \"{platform_sym}\""), |
| 226 | + Applicability::MachineApplicable, |
| 227 | + ) |
| 228 | + .emit(); |
| 229 | + } |
| 230 | + |
| 231 | + PlatformVersion::Apple { os: *platform_sym, version } |
| 232 | + } |
| 233 | + Err(error) => { |
| 234 | + sess.dcx().emit_err(session_diagnostics::AppleVersionInvalid { |
| 235 | + span: *version_span, |
| 236 | + error, |
| 237 | + }); |
| 238 | + return false; |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + // FIXME(madsmtm): Handle further platforms as specified in the RFC. |
| 243 | + sym::windows | sym::libc => { |
| 244 | + #[allow(rustc::untranslatable_diagnostic)] // Temporary |
| 245 | + dcx.span_err(*platform_span, "unimplemented platform"); |
| 246 | + return false; |
| 247 | + } |
| 248 | + _ => { |
| 249 | + // Unknown platform. This is intentionally a warning (and not an error) to be |
| 250 | + // future-compatible with later additions. |
| 251 | + let known_platforms = [ |
| 252 | + sym::macos, |
| 253 | + sym::ios, |
| 254 | + sym::tvos, |
| 255 | + sym::watchos, |
| 256 | + sym::visionos, |
| 257 | + // sym::windows, |
| 258 | + // sym::libc, |
| 259 | + ]; |
| 260 | + dcx.emit_warn(session_diagnostics::UnknownPlatformLiteral { |
| 261 | + span: *platform_span, |
| 262 | + possibilities: known_platforms.into_iter().collect(), |
| 263 | + }); |
| 264 | + return false; |
| 265 | + } |
| 266 | + }; |
| 267 | + |
| 268 | + // Figure out actual cfg-status based on current platform. |
| 269 | + match version { |
| 270 | + PlatformVersion::Apple { os, version } if os.as_str() == sess.target.os => { |
| 271 | + let deployment_target = sess.apple_deployment_target(); |
| 272 | + version <= deployment_target |
| 273 | + } |
| 274 | + // If a `cfg`-value does not apply to a specific platform, assume |
| 275 | + _ => false, |
| 276 | + } |
| 277 | + } |
153 | 278 | ast::MetaItemKind::List(mis) => {
|
154 | 279 | for mi in mis.iter() {
|
155 | 280 | if mi.meta_item_or_bool().is_none() {
|
@@ -251,3 +376,8 @@ pub fn eval_condition(
|
251 | 376 | }
|
252 | 377 | }
|
253 | 378 | }
|
| 379 | + |
| 380 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 381 | +enum PlatformVersion { |
| 382 | + Apple { os: Symbol, version: apple::OSVersion }, |
| 383 | +} |
0 commit comments