|
| 1 | +use crate::error::{TranslateError, TranslateErrorKind}; |
| 2 | +use crate::fluent_bundle::*; |
| 3 | +use crate::translation::Translate; |
| 4 | +use crate::FluentBundle; |
| 5 | +use rustc_data_structures::sync::Lrc; |
| 6 | +use rustc_error_messages::fluent_bundle::resolver::errors::{ReferenceKind, ResolverError}; |
| 7 | +use rustc_error_messages::langid; |
| 8 | +use rustc_error_messages::DiagnosticMessage; |
| 9 | + |
| 10 | +struct Dummy { |
| 11 | + bundle: FluentBundle, |
| 12 | +} |
| 13 | + |
| 14 | +impl Translate for Dummy { |
| 15 | + fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> { |
| 16 | + None |
| 17 | + } |
| 18 | + |
| 19 | + fn fallback_fluent_bundle(&self) -> &FluentBundle { |
| 20 | + &self.bundle |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn make_dummy(ftl: &'static str) -> Dummy { |
| 25 | + let resource = FluentResource::try_new(ftl.into()).expect("Failed to parse an FTL string."); |
| 26 | + |
| 27 | + let langid_en = langid!("en-US"); |
| 28 | + let mut bundle = FluentBundle::new(vec![langid_en]); |
| 29 | + |
| 30 | + bundle.add_resource(resource).expect("Failed to add FTL resources to the bundle."); |
| 31 | + |
| 32 | + Dummy { bundle } |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn wellformed_fluent() { |
| 37 | + let dummy = make_dummy("mir_build_borrow_of_moved_value = borrow of moved value |
| 38 | + .label = value moved into `{$name}` here |
| 39 | + .occurs_because_label = move occurs because `{$name}` has type `{$ty}` which does not implement the `Copy` trait |
| 40 | + .value_borrowed_label = value borrowed here after move |
| 41 | + .suggestion = borrow this binding in the pattern to avoid moving the value"); |
| 42 | + |
| 43 | + let mut args = FluentArgs::new(); |
| 44 | + args.set("name", "Foo"); |
| 45 | + args.set("ty", "std::string::String"); |
| 46 | + { |
| 47 | + let message = DiagnosticMessage::FluentIdentifier( |
| 48 | + "mir_build_borrow_of_moved_value".into(), |
| 49 | + Some("suggestion".into()), |
| 50 | + ); |
| 51 | + |
| 52 | + assert_eq!( |
| 53 | + dummy.translate_message(&message, &args).unwrap(), |
| 54 | + "borrow this binding in the pattern to avoid moving the value" |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + { |
| 59 | + let message = DiagnosticMessage::FluentIdentifier( |
| 60 | + "mir_build_borrow_of_moved_value".into(), |
| 61 | + Some("value_borrowed_label".into()), |
| 62 | + ); |
| 63 | + |
| 64 | + assert_eq!( |
| 65 | + dummy.translate_message(&message, &args).unwrap(), |
| 66 | + "value borrowed here after move" |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + { |
| 71 | + let message = DiagnosticMessage::FluentIdentifier( |
| 72 | + "mir_build_borrow_of_moved_value".into(), |
| 73 | + Some("occurs_because_label".into()), |
| 74 | + ); |
| 75 | + |
| 76 | + assert_eq!( |
| 77 | + dummy.translate_message(&message, &args).unwrap(), |
| 78 | + "move occurs because `\u{2068}Foo\u{2069}` has type `\u{2068}std::string::String\u{2069}` which does not implement the `Copy` trait" |
| 79 | + ); |
| 80 | + |
| 81 | + { |
| 82 | + let message = DiagnosticMessage::FluentIdentifier( |
| 83 | + "mir_build_borrow_of_moved_value".into(), |
| 84 | + Some("label".into()), |
| 85 | + ); |
| 86 | + |
| 87 | + assert_eq!( |
| 88 | + dummy.translate_message(&message, &args).unwrap(), |
| 89 | + "value moved into `\u{2068}Foo\u{2069}` here" |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[test] |
| 96 | +fn misformed_fluent() { |
| 97 | + let dummy = make_dummy("mir_build_borrow_of_moved_value = borrow of moved value |
| 98 | + .label = value moved into `{name}` here |
| 99 | + .occurs_because_label = move occurs because `{$oops}` has type `{$ty}` which does not implement the `Copy` trait |
| 100 | + .suggestion = borrow this binding in the pattern to avoid moving the value"); |
| 101 | + |
| 102 | + let mut args = FluentArgs::new(); |
| 103 | + args.set("name", "Foo"); |
| 104 | + args.set("ty", "std::string::String"); |
| 105 | + { |
| 106 | + let message = DiagnosticMessage::FluentIdentifier( |
| 107 | + "mir_build_borrow_of_moved_value".into(), |
| 108 | + Some("value_borrowed_label".into()), |
| 109 | + ); |
| 110 | + |
| 111 | + let err = dummy.translate_message(&message, &args).unwrap_err(); |
| 112 | + assert!( |
| 113 | + matches!( |
| 114 | + &err, |
| 115 | + TranslateError::Two { |
| 116 | + primary: box TranslateError::One { |
| 117 | + kind: TranslateErrorKind::PrimaryBundleMissing, |
| 118 | + .. |
| 119 | + }, |
| 120 | + fallback: box TranslateError::One { |
| 121 | + kind: TranslateErrorKind::AttributeMissing { attr: "value_borrowed_label" }, |
| 122 | + .. |
| 123 | + } |
| 124 | + } |
| 125 | + ), |
| 126 | + "{err:#?}" |
| 127 | + ); |
| 128 | + assert_eq!( |
| 129 | + format!("{err}"), |
| 130 | + "failed while formatting fluent string `mir_build_borrow_of_moved_value`: \nthe attribute `value_borrowed_label` was missing\nhelp: add `.value_borrowed_label = <message>`\n" |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + { |
| 135 | + let message = DiagnosticMessage::FluentIdentifier( |
| 136 | + "mir_build_borrow_of_moved_value".into(), |
| 137 | + Some("label".into()), |
| 138 | + ); |
| 139 | + |
| 140 | + let err = dummy.translate_message(&message, &args).unwrap_err(); |
| 141 | + if let TranslateError::Two { |
| 142 | + primary: box TranslateError::One { kind: TranslateErrorKind::PrimaryBundleMissing, .. }, |
| 143 | + fallback: box TranslateError::One { kind: TranslateErrorKind::Fluent { errs }, .. }, |
| 144 | + } = &err |
| 145 | + && let [FluentError::ResolverError(ResolverError::Reference( |
| 146 | + ReferenceKind::Message { id, .. } |
| 147 | + | ReferenceKind::Variable { id, .. }, |
| 148 | + ))] = &**errs |
| 149 | + && id == "name" |
| 150 | + {} else { |
| 151 | + panic!("{err:#?}") |
| 152 | + }; |
| 153 | + assert_eq!( |
| 154 | + format!("{err}"), |
| 155 | + "failed while formatting fluent string `mir_build_borrow_of_moved_value`: \nargument `name` exists but was not referenced correctly\nhelp: try using `{$name}` instead\n" |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + { |
| 160 | + let message = DiagnosticMessage::FluentIdentifier( |
| 161 | + "mir_build_borrow_of_moved_value".into(), |
| 162 | + Some("occurs_because_label".into()), |
| 163 | + ); |
| 164 | + |
| 165 | + let err = dummy.translate_message(&message, &args).unwrap_err(); |
| 166 | + if let TranslateError::Two { |
| 167 | + primary: box TranslateError::One { kind: TranslateErrorKind::PrimaryBundleMissing, .. }, |
| 168 | + fallback: box TranslateError::One { kind: TranslateErrorKind::Fluent { errs }, .. }, |
| 169 | + } = &err |
| 170 | + && let [FluentError::ResolverError(ResolverError::Reference( |
| 171 | + ReferenceKind::Message { id, .. } |
| 172 | + | ReferenceKind::Variable { id, .. }, |
| 173 | + ))] = &**errs |
| 174 | + && id == "oops" |
| 175 | + {} else { |
| 176 | + panic!("{err:#?}") |
| 177 | + }; |
| 178 | + assert_eq!( |
| 179 | + format!("{err}"), |
| 180 | + "failed while formatting fluent string `mir_build_borrow_of_moved_value`: \nthe fluent string has an argument `oops` that was not found.\nhelp: the arguments `name` and `ty` are available\n" |
| 181 | + ); |
| 182 | + } |
| 183 | +} |
0 commit comments