Skip to content

Commit 293cf51

Browse files
committed
rebase and review comments
1 parent 2db9b86 commit 293cf51

File tree

5 files changed

+41
-76
lines changed

5 files changed

+41
-76
lines changed

src/librustc_trait_selection/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(drain_filter)]
1616
#![feature(in_band_lifetimes)]
1717
#![feature(crate_visibility_modifier)]
18+
#![feature(or_patterns)]
1819
#![recursion_limit = "512"] // For rustdoc
1920

2021
#[macro_use]

src/librustc_trait_selection/traits/error_reporting/mod.rs

+13-36
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,14 @@ pub fn suggest_constraining_type_param(
16981698
err.span_label(param.span, &format!("this type parameter needs to be `{}`", constraint));
16991699
return true;
17001700
}
1701+
let mut suggest_restrict = |span| {
1702+
err.span_suggestion_verbose(
1703+
span,
1704+
MSG_RESTRICT_BOUND_FURTHER,
1705+
format!(" + {}", constraint),
1706+
Applicability::MachineApplicable,
1707+
);
1708+
};
17011709

17021710
if param_name.starts_with("impl ") {
17031711
// If there's an `impl Trait` used in argument position, suggest
@@ -1715,19 +1723,13 @@ pub fn suggest_constraining_type_param(
17151723
// |
17161724
// replace with: `impl Foo + Bar`
17171725

1718-
err.span_suggestion_verbose(
1719-
param.span.shrink_to_hi(),
1720-
MSG_RESTRICT_BOUND_FURTHER,
1721-
format!(" + {}", constraint),
1722-
Applicability::MachineApplicable,
1723-
);
1724-
1726+
suggest_restrict(param.span.shrink_to_hi());
17251727
return true;
17261728
}
17271729

17281730
if generics.where_clause.predicates.is_empty()
17291731
// Given `trait Base<T = String>: Super<T>` where `T: Copy`, suggest restricting in the
1730-
// `where` clause in stead of `trait Base<T: Copy = String>: Super<T>`.
1732+
// `where` clause instead of `trait Base<T: Copy = String>: Super<T>`.
17311733
&& !matches!(param.kind, hir::GenericParamKind::Type { default: Some(_), .. })
17321734
{
17331735
if let Some(bounds_span) = param.bounds_span() {
@@ -1744,13 +1746,7 @@ pub fn suggest_constraining_type_param(
17441746
// --
17451747
// |
17461748
// replace with: `T: Bar +`
1747-
1748-
err.span_suggestion_verbose(
1749-
bounds_span.shrink_to_hi(),
1750-
MSG_RESTRICT_BOUND_FURTHER,
1751-
format!(" + {}", constraint),
1752-
Applicability::MachineApplicable,
1753-
);
1749+
suggest_restrict(bounds_span.shrink_to_hi());
17541750
} else {
17551751
// If user hasn't provided any bounds, suggest adding a new one:
17561752
//
@@ -1827,9 +1823,7 @@ pub fn suggest_constraining_type_param(
18271823
// Account for `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
18281824
let mut trailing_comma = false;
18291825
if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(where_clause_span) {
1830-
if snippet.ends_with(",") {
1831-
trailing_comma = true;
1832-
}
1826+
trailing_comma = snippet.ends_with(",");
18331827
}
18341828
let where_clause_span = if trailing_comma {
18351829
let hi = where_clause_span.hi();
@@ -1839,24 +1833,7 @@ pub fn suggest_constraining_type_param(
18391833
};
18401834

18411835
match &param_spans[..] {
1842-
&[] => {
1843-
err.span_suggestion_verbose(
1844-
where_clause_span,
1845-
&msg_restrict_type_further,
1846-
format!(", {}: {}", param_name, constraint),
1847-
Applicability::MachineApplicable,
1848-
);
1849-
}
1850-
1851-
&[&param_span] => {
1852-
err.span_suggestion_verbose(
1853-
param_span.shrink_to_hi(),
1854-
MSG_RESTRICT_BOUND_FURTHER,
1855-
format!(" + {}", constraint),
1856-
Applicability::MachineApplicable,
1857-
);
1858-
}
1859-
1836+
&[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
18601837
_ => {
18611838
err.span_suggestion_verbose(
18621839
where_clause_span,

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+19-30
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
195195
return;
196196
}
197197

198-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. })
199-
| hir::Node::TraitItem(hir::TraitItem {
198+
hir::Node::TraitItem(hir::TraitItem {
200199
generics,
201200
kind: hir::TraitItemKind::Fn(..),
202201
..
@@ -206,39 +205,29 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
206205
kind: hir::ImplItemKind::Fn(..),
207206
..
208207
})
209-
| hir::Node::Item(hir::Item {
210-
kind: hir::ItemKind::Trait(_, _, generics, _, _),
211-
..
212-
})
213-
| hir::Node::Item(hir::Item {
214-
kind: hir::ItemKind::Impl { generics, .. }, ..
215-
}) if projection.is_some() => {
208+
| hir::Node::Item(
209+
hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }
210+
| hir::Item { kind: hir::ItemKind::Trait(_, _, generics, _, _), .. }
211+
| hir::Item { kind: hir::ItemKind::Impl { generics, .. }, .. },
212+
) if projection.is_some() => {
216213
// Missing associated type bound.
217214
suggest_restriction(&generics, "the associated type", err);
218215
return;
219216
}
220217

221-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Struct(_, generics), .. })
222-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Enum(_, generics), .. })
223-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Union(_, generics), .. })
224-
| hir::Node::Item(hir::Item {
225-
kind: hir::ItemKind::Trait(_, _, generics, ..),
226-
..
227-
})
228-
| hir::Node::Item(hir::Item {
229-
kind: hir::ItemKind::Impl { generics, .. }, ..
230-
})
231-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. })
232-
| hir::Node::Item(hir::Item {
233-
kind: hir::ItemKind::TyAlias(_, generics), ..
234-
})
235-
| hir::Node::Item(hir::Item {
236-
kind: hir::ItemKind::TraitAlias(generics, _), ..
237-
})
238-
| hir::Node::Item(hir::Item {
239-
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
240-
..
241-
})
218+
hir::Node::Item(
219+
hir::Item { kind: hir::ItemKind::Struct(_, generics), .. }
220+
| hir::Item { kind: hir::ItemKind::Enum(_, generics), .. }
221+
| hir::Item { kind: hir::ItemKind::Union(_, generics), .. }
222+
| hir::Item { kind: hir::ItemKind::Trait(_, _, generics, ..), .. }
223+
| hir::Item { kind: hir::ItemKind::Impl { generics, .. }, .. }
224+
| hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }
225+
| hir::Item { kind: hir::ItemKind::TyAlias(_, generics), .. }
226+
| hir::Item { kind: hir::ItemKind::TraitAlias(generics, _), .. }
227+
| hir::Item {
228+
kind: hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }), ..
229+
},
230+
)
242231
| hir::Node::TraitItem(hir::TraitItem { generics, .. })
243232
| hir::Node::ImplItem(hir::ImplItem { generics, .. })
244233
if param_ty =>

src/test/ui/const-generics/issues/issue-61336-2.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied
1212
LL | [x; { N }]
1313
| ^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `T`
1414
|
15-
help: consider restricting this type parameter with `T: std::marker::Copy`
16-
--> $DIR/issue-61336-2.rs:8:6
17-
|
18-
LL | fn g<T, const N: usize>(x: T) -> [T; N] {
19-
| ^
2015
= note: the `Copy` trait is required because the repeated element will be copied
16+
help: consider restricting type parameter `T`
17+
|
18+
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
19+
| ^^^^^^^^^^^^^^^^^^^
2120

2221
error: aborting due to previous error
2322

src/test/ui/const-generics/issues/issue-61336.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ error[E0277]: the trait bound `T: std::marker::Copy` is not satisfied
1212
LL | [x; N]
1313
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `T`
1414
|
15-
help: consider restricting this type parameter with `T: std::marker::Copy`
16-
--> $DIR/issue-61336.rs:8:6
17-
|
18-
LL | fn g<T, const N: usize>(x: T) -> [T; N] {
19-
| ^
2015
= note: the `Copy` trait is required because the repeated element will be copied
16+
help: consider restricting type parameter `T`
17+
|
18+
LL | fn g<T: std::marker::Copy, const N: usize>(x: T) -> [T; N] {
19+
| ^^^^^^^^^^^^^^^^^^^
2120

2221
error: aborting due to previous error
2322

0 commit comments

Comments
 (0)