summaryrefslogtreecommitdiff
path: root/compiler/rustc_errors/src/tests.rs
blob: 52103e4609770b50f3ae767ffb8ac754c8439059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::error::{TranslateError, TranslateErrorKind};
use crate::fluent_bundle::*;
use crate::translation::Translate;
use crate::FluentBundle;
use rustc_data_structures::sync::Lrc;
use rustc_error_messages::fluent_bundle::resolver::errors::{ReferenceKind, ResolverError};
use rustc_error_messages::langid;
use rustc_error_messages::DiagnosticMessage;

struct Dummy {
    bundle: FluentBundle,
}

impl Translate for Dummy {
    fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
        None
    }

    fn fallback_fluent_bundle(&self) -> &FluentBundle {
        &self.bundle
    }
}

fn make_dummy(ftl: &'static str) -> Dummy {
    let resource = FluentResource::try_new(ftl.into()).expect("Failed to parse an FTL string.");

    let langid_en = langid!("en-US");

    #[cfg(parallel_compiler)]
    let mut bundle = FluentBundle::new_concurrent(vec![langid_en]);

    #[cfg(not(parallel_compiler))]
    let mut bundle = FluentBundle::new(vec![langid_en]);

    bundle.add_resource(resource).expect("Failed to add FTL resources to the bundle.");

    Dummy { bundle }
}

#[test]
fn wellformed_fluent() {
    let dummy = make_dummy("mir_build_borrow_of_moved_value = borrow of moved value
    .label = value moved into `{$name}` here
    .occurs_because_label = move occurs because `{$name}` has type `{$ty}` which does not implement the `Copy` trait
    .value_borrowed_label = value borrowed here after move
    .suggestion = borrow this binding in the pattern to avoid moving the value");

    let mut args = FluentArgs::new();
    args.set("name", "Foo");
    args.set("ty", "std::string::String");
    {
        let message = DiagnosticMessage::FluentIdentifier(
            "mir_build_borrow_of_moved_value".into(),
            Some("suggestion".into()),
        );

        assert_eq!(
            dummy.translate_message(&message, &args).unwrap(),
            "borrow this binding in the pattern to avoid moving the value"
        );
    }

    {
        let message = DiagnosticMessage::FluentIdentifier(
            "mir_build_borrow_of_moved_value".into(),
            Some("value_borrowed_label".into()),
        );

        assert_eq!(
            dummy.translate_message(&message, &args).unwrap(),
            "value borrowed here after move"
        );
    }

    {
        let message = DiagnosticMessage::FluentIdentifier(
            "mir_build_borrow_of_moved_value".into(),
            Some("occurs_because_label".into()),
        );

        assert_eq!(
            dummy.translate_message(&message, &args).unwrap(),
            "move occurs because `\u{2068}Foo\u{2069}` has type `\u{2068}std::string::String\u{2069}` which does not implement the `Copy` trait"
        );

        {
            let message = DiagnosticMessage::FluentIdentifier(
                "mir_build_borrow_of_moved_value".into(),
                Some("label".into()),
            );

            assert_eq!(
                dummy.translate_message(&message, &args).unwrap(),
                "value moved into `\u{2068}Foo\u{2069}` here"
            );
        }
    }
}

#[test]
fn misformed_fluent() {
    let dummy = make_dummy("mir_build_borrow_of_moved_value = borrow of moved value
    .label = value moved into `{name}` here
    .occurs_because_label = move occurs because `{$oops}` has type `{$ty}` which does not implement the `Copy` trait
    .suggestion = borrow this binding in the pattern to avoid moving the value");

    let mut args = FluentArgs::new();
    args.set("name", "Foo");
    args.set("ty", "std::string::String");
    {
        let message = DiagnosticMessage::FluentIdentifier(
            "mir_build_borrow_of_moved_value".into(),
            Some("value_borrowed_label".into()),
        );

        let err = dummy.translate_message(&message, &args).unwrap_err();
        assert!(
            matches!(
                &err,
                TranslateError::Two {
                    primary: box TranslateError::One {
                        kind: TranslateErrorKind::PrimaryBundleMissing,
                        ..
                    },
                    fallback: box TranslateError::One {
                        kind: TranslateErrorKind::AttributeMissing { attr: "value_borrowed_label" },
                        ..
                    }
                }
            ),
            "{err:#?}"
        );
        assert_eq!(
            format!("{err}"),
            "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"
        );
    }

    {
        let message = DiagnosticMessage::FluentIdentifier(
            "mir_build_borrow_of_moved_value".into(),
            Some("label".into()),
        );

        let err = dummy.translate_message(&message, &args).unwrap_err();
        if let TranslateError::Two {
            primary: box TranslateError::One { kind: TranslateErrorKind::PrimaryBundleMissing, .. },
            fallback: box TranslateError::One { kind: TranslateErrorKind::Fluent { errs }, .. },
        } = &err
            && let [FluentError::ResolverError(ResolverError::Reference(
                ReferenceKind::Message { id, .. }
                    | ReferenceKind::Variable { id, .. },
            ))] = &**errs
            && id == "name"
        {} else {
            panic!("{err:#?}")
        };
        assert_eq!(
            format!("{err}"),
            "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"
        );
    }

    {
        let message = DiagnosticMessage::FluentIdentifier(
            "mir_build_borrow_of_moved_value".into(),
            Some("occurs_because_label".into()),
        );

        let err = dummy.translate_message(&message, &args).unwrap_err();
        if let TranslateError::Two {
            primary: box TranslateError::One { kind: TranslateErrorKind::PrimaryBundleMissing, .. },
            fallback: box TranslateError::One { kind: TranslateErrorKind::Fluent { errs }, .. },
        } = &err
            && let [FluentError::ResolverError(ResolverError::Reference(
                ReferenceKind::Message { id, .. }
                    | ReferenceKind::Variable { id, .. },
            ))] = &**errs
            && id == "oops"
        {} else {
            panic!("{err:#?}")
        };
        assert_eq!(
            format!("{err}"),
            "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"
        );
    }
}