Skip to content

Commit

Permalink
Refactored a few bits:
Browse files Browse the repository at this point in the history
- Firstly get all the information about generics matching out of the HIR
- Secondly the labelling for the function is more coherent now
- Lastly a few error message improvements
  • Loading branch information
strottos committed Mar 15, 2024
1 parent df93364 commit 8c2eff7
Show file tree
Hide file tree
Showing 17 changed files with 358 additions and 427 deletions.
35 changes: 34 additions & 1 deletion compiler/rustc_errors/src/lib.rs
Expand Up @@ -50,7 +50,7 @@ pub use rustc_error_messages::{
fallback_fluent_bundle, fluent_bundle, DelayDm, DiagMessage, FluentBundle, LanguageIdentifier,
LazyFallbackBundle, MultiSpan, SpanLabel, SubdiagMessage,
};
pub use rustc_lint_defs::{a_or_an, display_list_with_comma_and, pluralize, Applicability};
pub use rustc_lint_defs::{pluralize, Applicability};
pub use rustc_span::fatal_error::{FatalError, FatalErrorMarker};
pub use rustc_span::ErrorGuaranteed;
pub use snippet::Style;
Expand Down Expand Up @@ -1946,6 +1946,39 @@ pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
}
}

/// Grammatical tool for displaying messages to end users in a nice form.
///
/// Returns "an" if the given string starts with a vowel, and "a" otherwise.
pub fn a_or_an(s: &str) -> &'static str {
let mut chars = s.chars();
let Some(mut first_alpha_char) = chars.next() else {
return "a";
};
if first_alpha_char == '`' {
let Some(next) = chars.next() else {
return "a";
};
first_alpha_char = next;
}
if ["a", "e", "i", "o", "u", "&"].contains(&&first_alpha_char.to_lowercase().to_string()[..]) {
"an"
} else {
"a"
}
}

/// Grammatical tool for displaying messages to end users in a nice form.
///
/// Take a list ["a", "b", "c"] and output a display friendly version "a, b and c"
pub fn display_list_with_comma_and<T: std::fmt::Display>(v: &[T]) -> String {
match v.len() {
0 => "".to_string(),
1 => v[0].to_string(),
2 => format!("{} and {}", v[0], v[1]),
_ => format!("{}, {}", v[0], display_list_with_comma_and(&v[1..])),
}
}

#[derive(Clone, Copy, PartialEq, Hash, Debug)]
pub enum TerminalUrl {
No,
Expand Down
491 changes: 215 additions & 276 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Large diffs are not rendered by default.

33 changes: 0 additions & 33 deletions compiler/rustc_lint_defs/src/lib.rs
Expand Up @@ -39,39 +39,6 @@ macro_rules! pluralize {
};
}

/// Grammatical tool for displaying messages to end users in a nice form.
///
/// Returns "an" if the given string starts with a vowel, and "a" otherwise.
pub fn a_or_an(s: &str) -> &'static str {
let mut chars = s.chars();
let Some(mut first_alpha_char) = chars.next() else {
return "a";
};
if first_alpha_char == '`' {
let Some(next) = chars.next() else {
return "a";
};
first_alpha_char = next;
}
if ["a", "e", "i", "o", "u", "&"].contains(&&first_alpha_char.to_lowercase().to_string()[..]) {
"an"
} else {
"a"
}
}

/// Grammatical tool for displaying messages to end users in a nice form.
///
/// Take a list ["a", "b", "c"] and output a display friendly version "a, b and c"
pub fn display_list_with_comma_and<T: std::fmt::Display>(v: &[T]) -> String {
match v.len() {
0 => "".to_string(),
1 => v[0].to_string(),
2 => format!("{} and {}", v[0], v[1]),
_ => format!("{}, {}", v[0], display_list_with_comma_and(&v[1..])),
}
}

/// Indicates the confidence in the correctness of a suggestion.
///
/// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
Expand Down
14 changes: 7 additions & 7 deletions tests/ui/argument-suggestions/extra_arguments.stderr
Expand Up @@ -45,7 +45,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----

error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:23:3
Expand All @@ -60,7 +60,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----

error[E0061]: this function takes 1 argument but 3 arguments were supplied
--> $DIR/extra_arguments.rs:24:3
Expand All @@ -74,7 +74,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----
help: remove the extra arguments
|
LL - one_arg(1, "", 1.0);
Expand Down Expand Up @@ -319,7 +319,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----

error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:54:3
Expand All @@ -334,7 +334,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----

error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:55:3
Expand All @@ -349,7 +349,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----

error[E0061]: this function takes 1 argument but 2 arguments were supplied
--> $DIR/extra_arguments.rs:60:3
Expand All @@ -364,7 +364,7 @@ note: function defined here
--> $DIR/extra_arguments.rs:2:4
|
LL | fn one_arg<T>(_a: T) {}
| ^^^^^^^
| ^^^^^^^ -----

error: aborting due to 22 previous errors

Expand Down
20 changes: 10 additions & 10 deletions tests/ui/argument-suggestions/invalid_arguments.stderr
Expand Up @@ -24,7 +24,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:6:4
|
LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/invalid_arguments.rs:17:16
Expand All @@ -38,7 +38,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:6:4
|
LL | fn two_arg_same(_a: i32, _b: i32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^ -------

error[E0308]: arguments to this function are incorrect
--> $DIR/invalid_arguments.rs:18:3
Expand Down Expand Up @@ -66,7 +66,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:7:4
|
LL | fn two_arg_diff(_a: i32, _b: f32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/invalid_arguments.rs:20:16
Expand All @@ -80,7 +80,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:7:4
|
LL | fn two_arg_diff(_a: i32, _b: f32) {}
| ^^^^^^^^^^^^ ------- -------
| ^^^^^^^^^^^^ -------

error[E0308]: arguments to this function are incorrect
--> $DIR/invalid_arguments.rs:21:3
Expand Down Expand Up @@ -108,7 +108,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:8:4
|
LL | fn three_arg_diff(_a: i32, _b: f32, _c: &str) {}
| ^^^^^^^^^^^^^^ ------- ------- --------
| ^^^^^^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/invalid_arguments.rs:25:21
Expand All @@ -122,7 +122,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:8:4
|
LL | fn three_arg_diff(_a: i32, _b: f32, _c: &str) {}
| ^^^^^^^^^^^^^^ ------- ------- --------
| ^^^^^^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/invalid_arguments.rs:26:26
Expand All @@ -136,7 +136,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:8:4
|
LL | fn three_arg_diff(_a: i32, _b: f32, _c: &str) {}
| ^^^^^^^^^^^^^^ ------- ------- --------
| ^^^^^^^^^^^^^^ --------

error[E0308]: arguments to this function are incorrect
--> $DIR/invalid_arguments.rs:28:3
Expand Down Expand Up @@ -207,7 +207,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:9:4
|
LL | fn three_arg_repeat(_a: i32, _b: i32, _c: &str) {}
| ^^^^^^^^^^^^^^^^ ------- ------- --------
| ^^^^^^^^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/invalid_arguments.rs:35:23
Expand All @@ -221,7 +221,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:9:4
|
LL | fn three_arg_repeat(_a: i32, _b: i32, _c: &str) {}
| ^^^^^^^^^^^^^^^^ ------- ------- --------
| ^^^^^^^^^^^^^^^^ -------

error[E0308]: mismatched types
--> $DIR/invalid_arguments.rs:36:26
Expand All @@ -235,7 +235,7 @@ note: function defined here
--> $DIR/invalid_arguments.rs:9:4
|
LL | fn three_arg_repeat(_a: i32, _b: i32, _c: &str) {}
| ^^^^^^^^^^^^^^^^ ------- ------- --------
| ^^^^^^^^^^^^^^^^ --------

error[E0308]: arguments to this function are incorrect
--> $DIR/invalid_arguments.rs:38:3
Expand Down
24 changes: 1 addition & 23 deletions tests/ui/argument-suggestions/too-long.stderr
Expand Up @@ -11,31 +11,9 @@ note: method defined here
|
LL | fn foo(
| ^^^
LL | &self,
LL | a: i32,
| ------
LL | b: i32,
| ------
LL | c: i32,
| ------
LL | d: i32,
| ------
LL | e: i32,
| ------
...
LL | f: i32,
| ------
LL | g: i32,
| ------
LL | h: i32,
| ------
LL | i: i32,
| ------
LL | j: i32,
| ------
LL | k: i32,
| ------
LL | l: i32,
| ------
help: consider dereferencing the borrow
|
LL | qux.foo(a, b, c, d, e, *f, g, h, i, j, k, l);
Expand Down
27 changes: 12 additions & 15 deletions tests/ui/async-await/coroutine-desc.stderr
Expand Up @@ -5,7 +5,7 @@ LL | fun(async {}, async {});
| --- -------- ^^^^^^^^ expected `async` block, found a different `async` block
| | |
| | the expected `async` block
| | expected argument `f2` to be an `async` block because that argument needs to match the type of this parameter
| | expected all arguments to be `async` block because they need to match the type of this parameter
| arguments to this function are incorrect
|
= note: expected `async` block `{async block@$DIR/coroutine-desc.rs:10:9: 10:17}`
Expand All @@ -14,10 +14,9 @@ note: function defined here
--> $DIR/coroutine-desc.rs:8:4
|
LL | fn fun<F: Future<Output = ()>>(f1: F, f2: F) {}
| ^^^ - ----- -----
| | | |
| | | this parameter needs to match the `async` block type of `f1`
| | `f2` needs to match the type of this parameter
| ^^^ - ----- ----- this parameter needs to match the `async` block type of `f1`
| | |
| | `f2` needs to match the `async` block type of this parameter
| `f1` and `f2` all reference this parameter F

error[E0308]: mismatched types
Expand All @@ -26,7 +25,7 @@ error[E0308]: mismatched types
LL | fun(one(), two());
| --- ----- ^^^^^ expected future, found a different future
| | |
| | expected argument `f2` to be a future because that argument needs to match the type of this parameter
| | expected all arguments to be future because they need to match the type of this parameter
| arguments to this function are incorrect
|
= help: consider `await`ing on both `Future`s
Expand All @@ -35,10 +34,9 @@ note: function defined here
--> $DIR/coroutine-desc.rs:8:4
|
LL | fn fun<F: Future<Output = ()>>(f1: F, f2: F) {}
| ^^^ - ----- -----
| | | |
| | | this parameter needs to match the future type of `f1`
| | `f2` needs to match the type of this parameter
| ^^^ - ----- ----- this parameter needs to match the future type of `f1`
| | |
| | `f2` needs to match the future type of this parameter
| `f1` and `f2` all reference this parameter F

error[E0308]: mismatched types
Expand All @@ -48,7 +46,7 @@ LL | fun((async || {})(), (async || {})());
| --- --------------- ^^^^^^^^^^^^^^^ expected `async` closure body, found a different `async` closure body
| | | |
| | | the expected `async` closure body
| | expected argument `f2` to be an `async` closure body because that argument needs to match the type of this parameter
| | expected all arguments to be `async` closure body because they need to match the type of this parameter
| arguments to this function are incorrect
|
= note: expected `async` closure body `{async closure body@$DIR/coroutine-desc.rs:14:19: 14:21}`
Expand All @@ -57,10 +55,9 @@ note: function defined here
--> $DIR/coroutine-desc.rs:8:4
|
LL | fn fun<F: Future<Output = ()>>(f1: F, f2: F) {}
| ^^^ - ----- -----
| | | |
| | | this parameter needs to match the `async` closure body type of `f1`
| | `f2` needs to match the type of this parameter
| ^^^ - ----- ----- this parameter needs to match the `async` closure body type of `f1`
| | |
| | `f2` needs to match the `async` closure body type of this parameter
| `f1` and `f2` all reference this parameter F

error: aborting due to 3 previous errors
Expand Down
9 changes: 4 additions & 5 deletions tests/ui/coercion/coerce-reborrow-multi-arg-fail.stderr
Expand Up @@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | test(&mut 7, &7);
| ---- ------ ^^ types differ in mutability
| | |
| | expected argument `_b` to be an `&mut {integer}` because that argument needs to match the type of this parameter
| | expected all arguments to be `&mut {integer}` because they need to match the type of this parameter
| arguments to this function are incorrect
|
= note: expected mutable reference `&mut {integer}`
Expand All @@ -13,10 +13,9 @@ note: function defined here
--> $DIR/coerce-reborrow-multi-arg-fail.rs:1:4
|
LL | fn test<T>(_a: T, _b: T) {}
| ^^^^ - ----- -----
| | | |
| | | this parameter needs to match the `&mut {integer}` type of `_a`
| | `_b` needs to match the type of this parameter
| ^^^^ - ----- ----- this parameter needs to match the `&mut {integer}` type of `_a`
| | |
| | `_b` needs to match the `&mut {integer}` type of this parameter
| `_a` and `_b` all reference this parameter T

error: aborting due to 1 previous error
Expand Down

0 comments on commit 8c2eff7

Please sign in to comment.