1 use std::num::NonZeroU32;
3 use crate::cgu_reuse_tracker::CguReuse;
4 use crate::parse::ParseSess;
6 use rustc_ast::util::literal::LitError;
7 use rustc_errors::MultiSpan;
8 use rustc_macros::Diagnostic;
9 use rustc_span::{Span, Symbol};
10 use rustc_target::spec::{SplitDebuginfo, StackProtector, TargetTriple};
13 #[diag(session_incorrect_cgu_reuse_type)]
14 pub struct IncorrectCguReuseType<'a> {
17 pub cgu_user_name: &'a str,
18 pub actual_reuse: CguReuse,
19 pub expected_reuse: CguReuse,
24 #[diag(session_cgu_not_recorded)]
25 pub struct CguNotRecorded<'a> {
26 pub cgu_user_name: &'a str,
27 pub cgu_name: &'a str,
31 #[diag(session_feature_gate_error, code = "E0658")]
32 pub struct FeatureGateError<'a> {
38 #[derive(Subdiagnostic)]
39 #[note(session_feature_diagnostic_for_issue)]
40 pub struct FeatureDiagnosticForIssue {
44 #[derive(Subdiagnostic)]
45 #[help(session_feature_diagnostic_help)]
46 pub struct FeatureDiagnosticHelp {
51 #[diag(session_not_circumvent_feature)]
52 pub struct NotCircumventFeature;
55 #[diag(session_linker_plugin_lto_windows_not_supported)]
56 pub struct LinkerPluginToWindowsNotSupported;
59 #[diag(session_profile_use_file_does_not_exist)]
60 pub struct ProfileUseFileDoesNotExist<'a> {
61 pub path: &'a std::path::Path,
65 #[diag(session_profile_sample_use_file_does_not_exist)]
66 pub struct ProfileSampleUseFileDoesNotExist<'a> {
67 pub path: &'a std::path::Path,
71 #[diag(session_target_requires_unwind_tables)]
72 pub struct TargetRequiresUnwindTables;
75 #[diag(session_sanitizer_not_supported)]
76 pub struct SanitizerNotSupported {
81 #[diag(session_sanitizers_not_supported)]
82 pub struct SanitizersNotSupported {
87 #[diag(session_cannot_mix_and_match_sanitizers)]
88 pub struct CannotMixAndMatchSanitizers {
94 #[diag(session_cannot_enable_crt_static_linux)]
95 pub struct CannotEnableCrtStaticLinux;
98 #[diag(session_sanitizer_cfi_enabled)]
99 pub struct SanitizerCfiEnabled;
101 #[derive(Diagnostic)]
102 #[diag(session_unstable_virtual_function_elimination)]
103 pub struct UnstableVirtualFunctionElimination;
105 #[derive(Diagnostic)]
106 #[diag(session_unsupported_dwarf_version)]
107 pub struct UnsupportedDwarfVersion {
108 pub dwarf_version: u32,
111 #[derive(Diagnostic)]
112 #[diag(session_target_stack_protector_not_supported)]
113 pub struct StackProtectorNotSupportedForTarget<'a> {
114 pub stack_protector: StackProtector,
115 pub target_triple: &'a TargetTriple,
118 #[derive(Diagnostic)]
119 #[diag(session_branch_protection_requires_aarch64)]
120 pub(crate) struct BranchProtectionRequiresAArch64;
122 #[derive(Diagnostic)]
123 #[diag(session_split_debuginfo_unstable_platform)]
124 pub struct SplitDebugInfoUnstablePlatform {
125 pub debuginfo: SplitDebuginfo,
128 #[derive(Diagnostic)]
129 #[diag(session_file_is_not_writeable)]
130 pub struct FileIsNotWriteable<'a> {
131 pub file: &'a std::path::Path,
134 #[derive(Diagnostic)]
135 #[diag(session_crate_name_does_not_match)]
136 pub struct CrateNameDoesNotMatch {
143 #[derive(Diagnostic)]
144 #[diag(session_crate_name_invalid)]
145 pub struct CrateNameInvalid<'a> {
149 #[derive(Diagnostic)]
150 #[diag(session_crate_name_empty)]
151 pub struct CrateNameEmpty {
153 pub span: Option<Span>,
156 #[derive(Diagnostic)]
157 #[diag(session_invalid_character_in_create_name)]
158 pub struct InvalidCharacterInCrateName {
160 pub span: Option<Span>,
162 pub crate_name: Symbol,
165 #[derive(Subdiagnostic)]
166 #[multipart_suggestion(session_expr_parentheses_needed, applicability = "machine-applicable")]
167 pub struct ExprParenthesesNeeded {
168 #[suggestion_part(code = "(")]
170 #[suggestion_part(code = ")")]
174 impl ExprParenthesesNeeded {
175 pub fn surrounding(s: Span) -> Self {
176 ExprParenthesesNeeded { left: s.shrink_to_lo(), right: s.shrink_to_hi() }
180 #[derive(Diagnostic)]
181 #[diag(session_skipping_const_checks)]
182 pub struct SkippingConstChecks {
184 pub unleashed_features: Vec<UnleashedFeatureHelp>,
187 #[derive(Subdiagnostic)]
188 pub enum UnleashedFeatureHelp {
189 #[help(session_unleashed_feature_help_named)]
195 #[help(session_unleashed_feature_help_unnamed)]
202 #[derive(Diagnostic)]
203 #[diag(session_invalid_literal_suffix)]
204 pub(crate) struct InvalidLiteralSuffix<'a> {
213 #[derive(Diagnostic)]
214 #[diag(session_invalid_int_literal_width)]
216 pub(crate) struct InvalidIntLiteralWidth {
222 #[derive(Diagnostic)]
223 #[diag(session_invalid_num_literal_base_prefix)]
225 pub(crate) struct InvalidNumLiteralBasePrefix {
227 #[suggestion(applicability = "maybe-incorrect", code = "{fixed}")]
232 #[derive(Diagnostic)]
233 #[diag(session_invalid_num_literal_suffix)]
235 pub(crate) struct InvalidNumLiteralSuffix {
242 #[derive(Diagnostic)]
243 #[diag(session_invalid_float_literal_width)]
245 pub(crate) struct InvalidFloatLiteralWidth {
251 #[derive(Diagnostic)]
252 #[diag(session_invalid_float_literal_suffix)]
254 pub(crate) struct InvalidFloatLiteralSuffix {
261 #[derive(Diagnostic)]
262 #[diag(session_int_literal_too_large)]
264 pub(crate) struct IntLiteralTooLarge {
270 #[derive(Diagnostic)]
271 #[diag(session_hexadecimal_float_literal_not_supported)]
272 pub(crate) struct HexadecimalFloatLiteralNotSupported {
274 #[label(session_not_supported)]
278 #[derive(Diagnostic)]
279 #[diag(session_octal_float_literal_not_supported)]
280 pub(crate) struct OctalFloatLiteralNotSupported {
282 #[label(session_not_supported)]
286 #[derive(Diagnostic)]
287 #[diag(session_binary_float_literal_not_supported)]
288 pub(crate) struct BinaryFloatLiteralNotSupported {
290 #[label(session_not_supported)]
294 pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span: Span) {
295 // Checks if `s` looks like i32 or u1234 etc.
296 fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
297 s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
300 // Try to lowercase the prefix if the prefix and suffix are valid.
301 fn fix_base_capitalisation(prefix: &str, suffix: &str) -> Option<String> {
302 let mut chars = suffix.chars();
304 let base_char = chars.next().unwrap();
305 let base = match base_char {
312 // check that the suffix contains only base-appropriate characters
313 let valid = prefix == "0"
315 .filter(|c| *c != '_')
316 .take_while(|c| *c != 'i' && *c != 'u')
317 .all(|c| c.to_digit(base).is_some());
320 Some(format!("0{}{}", base_char.to_ascii_lowercase(), &suffix[1..]))
326 let token::Lit { kind, symbol, suffix, .. } = lit;
328 // `LexerError` is an error, but it was already reported
329 // by lexer, so here we don't report it the second time.
330 LitError::LexerError => {}
331 LitError::InvalidSuffix => {
332 if let Some(suffix) = suffix {
333 sess.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix });
336 LitError::InvalidIntSuffix => {
337 let suf = suffix.expect("suffix error with no suffix");
338 let suf = suf.as_str();
339 if looks_like_width_suffix(&['i', 'u'], suf) {
340 // If it looks like a width, try to be helpful.
341 sess.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() });
342 } else if let Some(fixed) = fix_base_capitalisation(symbol.as_str(), suf) {
343 sess.emit_err(InvalidNumLiteralBasePrefix { span, fixed });
345 sess.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() });
348 LitError::InvalidFloatSuffix => {
349 let suf = suffix.expect("suffix error with no suffix");
350 let suf = suf.as_str();
351 if looks_like_width_suffix(&['f'], suf) {
352 // If it looks like a width, try to be helpful.
353 sess.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() });
355 sess.emit_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() });
358 LitError::NonDecimalFloat(base) => {
360 16 => sess.emit_err(HexadecimalFloatLiteralNotSupported { span }),
361 8 => sess.emit_err(OctalFloatLiteralNotSupported { span }),
362 2 => sess.emit_err(BinaryFloatLiteralNotSupported { span }),
366 LitError::IntTooLarge(base) => {
368 let limit = match base {
369 2 => format!("{max:#b}"),
370 8 => format!("{max:#o}"),
371 16 => format!("{max:#x}"),
372 _ => format!("{max}"),
374 sess.emit_err(IntLiteralTooLarge { span, limit });