Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #95990

Merged
merged 30 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
285b9d1
Delay a bug when we see SelfCtor in ref pattern
compiler-errors Apr 11, 2022
7d5bbf5
prevent opaque types from appearing in impl headers
lqd Jul 22, 2021
d652cd4
add regression tests for opaque types in impl headers
lqd Jul 22, 2021
734adb3
update ui tests using opaque types in impl headers
lqd Jul 22, 2021
de2edb2
Fix wrong suggestions for `T:`
WaffleLapkin Apr 12, 2022
e4710fe
Add test for `T:` suggestions
WaffleLapkin Apr 12, 2022
08ef70b
Compute a more precise span for opaque type impls
oli-obk Apr 12, 2022
93a3cfb
Explain the span search logic
oli-obk Apr 12, 2022
8de453a
rustdoc: discr. required+provided assoc consts+tys
fmease Mar 29, 2022
a21238c
Autotag library PRs with T-libs
yaahc Apr 12, 2022
e9a52c2
Move ident resolution to a dedicated module.
cjgillot Mar 26, 2022
eb7f567
Simplify binding finalization.
cjgillot Apr 8, 2022
24b37a7
Pass last_import_segment and unusable_binding as parameters.
cjgillot Apr 8, 2022
55ca03c
Insert error after checking for binding usability.
cjgillot Apr 8, 2022
3ee6f6e
Do not record Res when builing a suggestion.
cjgillot Apr 8, 2022
886613c
Make the logic more explicit with let chains.
cjgillot Apr 8, 2022
d1c1bbe
Move path resolution error to rustc_resolve::diagnostics.
cjgillot Apr 8, 2022
944d852
Simplify error reporting.
cjgillot Apr 8, 2022
abbd0b8
Move diagnostic methods to the dedicated module.
cjgillot Apr 8, 2022
b796d92
Fix imports.
cjgillot Apr 12, 2022
23bf977
Implement tuples using recursion
c410-f3r Apr 12, 2022
6d0349d
Apply suggestions from code review
oli-obk Apr 12, 2022
276b946
Handle `unusable_binding` more compactly.
petrochenkov Apr 12, 2022
0ec00c0
Rollup merge of #95316 - fmease:rustdoc-discr-req-prov-assoc-consts-t…
Dylan-DPC Apr 12, 2022
2743c13
Rollup merge of #95405 - cjgillot:probe, r=petrochenkov
Dylan-DPC Apr 12, 2022
6aa875a
Rollup merge of #95914 - c410-f3r:meta-vars, r=petrochenkov
Dylan-DPC Apr 12, 2022
91813a7
Rollup merge of #95918 - compiler-errors:issue-95878, r=cjgillot
Dylan-DPC Apr 12, 2022
d63a46a
Rollup merge of #95970 - WaffleLapkin:nicer_trait_suggestions, r=comp…
Dylan-DPC Apr 12, 2022
e96304b
Rollup merge of #95973 - oli-obk:tait_ub3, r=compiler-errors
Dylan-DPC Apr 12, 2022
bdbc398
Rollup merge of #95986 - yaahc:libs-autolabel, r=Mark-Simulacrum
Dylan-DPC Apr 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_error_messages::MultiSpan;
use rustc_index::vec::IndexVec;
use rustc_macros::HashStable_Generic;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::Spanned;
use rustc_span::source_map::{SourceMap, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{def_id::LocalDefId, BytePos, Span, DUMMY_SP};
use rustc_target::asm::InlineAsmRegOrRegClass;
Expand Down Expand Up @@ -523,6 +523,40 @@ impl<'hir> GenericParam<'hir> {
})
.map(|sp| sp.shrink_to_hi())
}

/// Returns the span of `:` after a generic parameter.
///
/// For example:
///
/// ```text
/// fn a<T:>()
/// ^
/// | here
/// here |
/// v
/// fn b<T :>()
///
/// fn c<T
///
/// :>()
/// ^
/// |
/// here
/// ```
pub fn colon_span_for_suggestions(&self, source_map: &SourceMap) -> Option<Span> {
let sp = source_map
.span_extend_while(self.span.shrink_to_hi(), |c| c.is_whitespace() || c == ':')
.ok()?;

let snippet = source_map.span_to_snippet(sp).ok()?;
let offset = snippet.find(':')?;

let colon_sp = sp
.with_lo(BytePos(sp.lo().0 + offset as u32))
.with_hi(BytePos(sp.lo().0 + (offset + ':'.len_utf8()) as u32));

Some(colon_sp)
}
}

#[derive(Default)]
Expand Down
35 changes: 28 additions & 7 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,14 @@ pub fn suggest_constraining_type_params<'a>(
}

let constraint = constraints.iter().map(|&(c, _)| c).collect::<Vec<_>>().join(" + ");
let mut suggest_restrict = |span| {
let mut suggest_restrict = |span, bound_list_non_empty| {
suggestions.push((
span,
format!(" + {}", constraint),
if bound_list_non_empty {
format!(" + {}", constraint)
} else {
format!(" {}", constraint)
},
SuggestChangingConstraintsMessage::RestrictBoundFurther,
))
};
Expand All @@ -360,7 +364,10 @@ pub fn suggest_constraining_type_params<'a>(
// |
// replace with: `impl Foo + Bar`

suggest_restrict(param.span.shrink_to_hi());
// `impl Trait` must have at least one trait in the list
let bound_list_non_empty = true;

suggest_restrict(param.span.shrink_to_hi(), bound_list_non_empty);
continue;
}

Expand All @@ -383,15 +390,25 @@ pub fn suggest_constraining_type_params<'a>(
// --
// |
// replace with: `T: Bar +`
suggest_restrict(span);

// `bounds_span_for_suggestions` returns `None` if the list is empty
let bound_list_non_empty = true;

suggest_restrict(span, bound_list_non_empty);
} else {
let (colon, span) = match param.colon_span_for_suggestions(tcx.sess.source_map()) {
// If there is already a colon after generic, do not suggest adding it again
Some(sp) => ("", sp.shrink_to_hi()),
None => (":", param.span.shrink_to_hi()),
};

// If user hasn't provided any bounds, suggest adding a new one:
//
// fn foo<T>(t: T) { ... }
// - help: consider restricting this type parameter with `T: Foo`
suggestions.push((
param.span.shrink_to_hi(),
format!(": {}", constraint),
span,
format!("{colon} {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
}
Expand Down Expand Up @@ -459,17 +476,21 @@ pub fn suggest_constraining_type_params<'a>(
));
} else {
let mut param_spans = Vec::new();
let mut non_empty = false;

for predicate in generics.where_clause.predicates {
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
span,
bounded_ty,
bounds,
..
}) = predicate
{
if let TyKind::Path(QPath::Resolved(_, path)) = &bounded_ty.kind {
if let Some(segment) = path.segments.first() {
if segment.ident.to_string() == param_name {
non_empty = !bounds.is_empty();

param_spans.push(span);
}
}
Expand All @@ -478,7 +499,7 @@ pub fn suggest_constraining_type_params<'a>(
}

match param_spans[..] {
[&param_span] => suggest_restrict(param_span.shrink_to_hi()),
[&param_span] => suggest_restrict(param_span.shrink_to_hi(), non_empty),
_ => {
suggestions.push((
generics.where_clause.tail_span_for_suggestion(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
Some(TypeNS),
parent_scope,
if finalize { Finalize::SimplePath(id, path.span) } else { Finalize::No },
None,
) {
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
let res = module.res().expect("visibility resolved to unnamed block");
Expand Down Expand Up @@ -1124,12 +1125,11 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
});
} else {
for ident in single_imports.iter().cloned() {
let result = self.r.resolve_ident_in_module(
let result = self.r.maybe_resolve_ident_in_module(
ModuleOrUniformRoot::Module(module),
ident,
MacroNS,
&self.parent_scope,
None,
);
if let Ok(binding) = result {
let import = macro_use_import(self, ident.span);
Expand Down
Loading