Skip to content

Commit f72cd8e

Browse files
authored
Unrolled build for rust-lang#121595
Rollup merge of rust-lang#121595 - strottos:issue_116615, r=compiler-errors Better reporting on generic argument mismatchs This allows better reporting as per issue rust-lang#116615 . If you have a function: ``` fn foo(a: T, b: T) {} ``` and call it like so: ``` foo(1, 2.) ``` it'll give improved error reported similar to the following: ``` error[E0308]: mismatched types --> generic-mismatch-reporting-issue-116615.rs:6:12 | 6 | foo(1, 2.); | --- - ^^ expected integer, found floating-point number | | | | | expected argument `b` to be an integer because that argument needs to match the type of this parameter | arguments to this function are incorrect | note: function defined here --> generic-mismatch-reporting-issue-116615.rs:1:4 | 1 | fn foo<T>(a: T, b: T) {} | ^^^ - ---- ---- | | | | | | | this parameter needs to match the integer type of `a` | | `b` needs to match the type of this parameter | `a` and `b` all reference this parameter T ``` Open question, do we need to worry about error message translation into other languages? Not sure what the status of that is in Rust. NB: Needs some checking over and some tests have altered that need sanity checking, but overall this is starting to get somewhere now. Will take out of draft PR status when this has been done, raising now to allow feedback at this stage, probably 90% ready.
2 parents 98efd80 + 8a5245e commit f72cd8e

File tree

7 files changed

+505
-38
lines changed

7 files changed

+505
-38
lines changed

compiler/rustc_errors/src/lib.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,39 @@ pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
19511951
}
19521952
}
19531953

1954+
/// Grammatical tool for displaying messages to end users in a nice form.
1955+
///
1956+
/// Returns "an" if the given string starts with a vowel, and "a" otherwise.
1957+
pub fn a_or_an(s: &str) -> &'static str {
1958+
let mut chars = s.chars();
1959+
let Some(mut first_alpha_char) = chars.next() else {
1960+
return "a";
1961+
};
1962+
if first_alpha_char == '`' {
1963+
let Some(next) = chars.next() else {
1964+
return "a";
1965+
};
1966+
first_alpha_char = next;
1967+
}
1968+
if ["a", "e", "i", "o", "u", "&"].contains(&&first_alpha_char.to_lowercase().to_string()[..]) {
1969+
"an"
1970+
} else {
1971+
"a"
1972+
}
1973+
}
1974+
1975+
/// Grammatical tool for displaying messages to end users in a nice form.
1976+
///
1977+
/// Take a list ["a", "b", "c"] and output a display friendly version "a, b and c"
1978+
pub fn display_list_with_comma_and<T: std::fmt::Display>(v: &[T]) -> String {
1979+
match v.len() {
1980+
0 => "".to_string(),
1981+
1 => v[0].to_string(),
1982+
2 => format!("{} and {}", v[0], v[1]),
1983+
_ => format!("{}, {}", v[0], display_list_with_comma_and(&v[1..])),
1984+
}
1985+
}
1986+
19541987
#[derive(Clone, Copy, PartialEq, Hash, Debug)]
19551988
pub enum TerminalUrl {
19561989
No,

0 commit comments

Comments
 (0)