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 9 pull requests #106193

Merged
merged 22 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3cddc8b
More inference-friendly API for lazy
matklad Oct 29, 2022
94d6245
Fix `explicit_outlives_requirements` lint in macros
Dec 22, 2022
83e6539
document that `Span::to` can go backwards
Dec 25, 2022
1eba6c4
address review comments + better tests
Dec 25, 2022
c9381fc
Detect likely `.` -> `..` typo in method calls
estebank Dec 16, 2022
9f18cc9
Fix a formatting error
DexterHaxxor Dec 27, 2022
ce1e6a4
Fix doc comment parsing
kraktus Dec 27, 2022
3cf22de
Suggest rewriting a malformed hex literal if we expect a float
compiler-errors Dec 17, 2022
e5c159c
Provide local extern function arg names
compiler-errors Dec 27, 2022
0c0685b
review comments: make suggestion more accurate
estebank Dec 27, 2022
4df5459
Update the documentation of `Vec` to use `extend(array)` instead of `…
ChayimFriedman2 Dec 27, 2022
b026167
Fix UnsafeCell Documentation Spelling Error
alexhrao Dec 27, 2022
7e84273
Make resolve suggestion more generic
estebank Dec 27, 2022
4b668a1
Rollup merge of #103718 - matklad:infer-lazy, r=dtolnay
compiler-errors Dec 27, 2022
a9fdedd
Rollup merge of #105765 - estebank:range-typo, r=compiler-errors
compiler-errors Dec 27, 2022
996fb66
Rollup merge of #105852 - compiler-errors:hex-float-lit, r=cjgillot
compiler-errors Dec 27, 2022
3fba7b4
Rollup merge of #105965 - compiler-errors:issue-105896, r=cjgillot
compiler-errors Dec 27, 2022
18bf19c
Rollup merge of #106064 - lukas-code:outlives-macro, r=cjgillot
compiler-errors Dec 27, 2022
7f5f31b
Rollup merge of #106179 - RetroSeven:typo_fix, r=compiler-errors
compiler-errors Dec 27, 2022
4de5547
Rollup merge of #106181 - kraktus:fix_doc_parsing, r=notriddle
compiler-errors Dec 27, 2022
79730d6
Rollup merge of #106187 - ChayimFriedman2:patch-4, r=compiler-errors
compiler-errors Dec 27, 2022
49d4346
Rollup merge of #106189 - alexhrao:master, r=Nilstrieb
compiler-errors Dec 27, 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
30 changes: 15 additions & 15 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
param.id,
&param.kind,
&param.bounds,
param.colon_span,
generics.span,
itctx,
PredicateOrigin::GenericParam,
)
Expand Down Expand Up @@ -1365,6 +1367,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
id: NodeId,
kind: &GenericParamKind,
bounds: &[GenericBound],
colon_span: Option<Span>,
parent_span: Span,
itctx: &ImplTraitContext,
origin: PredicateOrigin,
) -> Option<hir::WherePredicate<'hir>> {
Expand All @@ -1377,21 +1381,17 @@ impl<'hir> LoweringContext<'_, 'hir> {

let ident = self.lower_ident(ident);
let param_span = ident.span;
let span = bounds
.iter()
.fold(Some(param_span.shrink_to_hi()), |span: Option<Span>, bound| {
let bound_span = bound.span();
// We include bounds that come from a `#[derive(_)]` but point at the user's code,
// as we use this method to get a span appropriate for suggestions.
if !bound_span.can_be_used_for_suggestions() {
None
} else if let Some(span) = span {
Some(span.to(bound_span))
} else {
Some(bound_span)
}
})
.unwrap_or(param_span.shrink_to_hi());

// Reconstruct the span of the entire predicate from the individual generic bounds.
let span_start = colon_span.unwrap_or_else(|| param_span.shrink_to_hi());
let span = bounds.iter().fold(span_start, |span_accum, bound| {
match bound.span().find_ancestor_inside(parent_span) {
Some(bound_span) => span_accum.to(bound_span),
None => span_accum,
}
});
let span = self.lower_span(span);

match kind {
GenericParamKind::Const { .. } => None,
GenericParamKind::Type { .. } => {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2245,14 +2245,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
) -> (hir::GenericParam<'hir>, Option<hir::WherePredicate<'hir>>, hir::TyKind<'hir>) {
// Add a definition for the in-band `Param`.
let def_id = self.local_def_id(node_id);
let span = self.lower_span(span);

// Set the name to `impl Bound1 + Bound2`.
let param = hir::GenericParam {
hir_id: self.lower_node_id(node_id),
def_id,
name: ParamName::Plain(self.lower_ident(ident)),
pure_wrt_drop: false,
span: self.lower_span(span),
span,
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
colon_span: None,
};
Expand All @@ -2262,6 +2263,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
node_id,
&GenericParamKind::Type { default: None },
bounds,
/* colon_span */ None,
span,
&ImplTraitContext::Universal,
hir::PredicateOrigin::ImplTrait,
);
Expand All @@ -2271,7 +2274,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let ty = hir::TyKind::Path(hir::QPath::Resolved(
None,
self.arena.alloc(hir::Path {
span: self.lower_span(span),
span,
res,
segments:
arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
Expand Down
66 changes: 66 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.note_type_is_not_clone(err, expected, expr_ty, expr);
self.note_need_for_fn_pointer(err, expected, expr_ty);
self.note_internal_mutation_in_method(err, expr, expected, expr_ty);
self.check_for_range_as_method_call(err, expr, expr_ty, expected);
}

/// Requires that the two types unify, and prints an error message if
Expand Down Expand Up @@ -1607,4 +1608,69 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => false,
}
}

/// Identify when the user has written `foo..bar()` instead of `foo.bar()`.
pub fn check_for_range_as_method_call(
&self,
err: &mut Diagnostic,
expr: &hir::Expr<'_>,
checked_ty: Ty<'tcx>,
expected_ty: Ty<'tcx>,
) {
if !hir::is_range_literal(expr) {
return;
}
let hir::ExprKind::Struct(
hir::QPath::LangItem(LangItem::Range, ..),
[start, end],
_,
) = expr.kind else { return; };
let parent = self.tcx.hir().get_parent_node(expr.hir_id);
if let Some(hir::Node::ExprField(_)) = self.tcx.hir().find(parent) {
// Ignore `Foo { field: a..Default::default() }`
return;
}
let mut expr = end.expr;
while let hir::ExprKind::MethodCall(_, rcvr, ..) = expr.kind {
// Getting to the root receiver and asserting it is a fn call let's us ignore cases in
// `src/test/ui/methods/issues/issue-90315.stderr`.
expr = rcvr;
}
let hir::ExprKind::Call(method_name, _) = expr.kind else { return; };
let ty::Adt(adt, _) = checked_ty.kind() else { return; };
if self.tcx.lang_items().range_struct() != Some(adt.did()) {
return;
}
if let ty::Adt(adt, _) = expected_ty.kind()
&& self.tcx.lang_items().range_struct() == Some(adt.did())
{
return;
}
// Check if start has method named end.
let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = method_name.kind else { return; };
let [hir::PathSegment { ident, .. }] = p.segments else { return; };
let self_ty = self.typeck_results.borrow().expr_ty(start.expr);
let Ok(_pick) = self.probe_for_name(
probe::Mode::MethodCall,
*ident,
probe::IsSuggestion(true),
self_ty,
expr.hir_id,
probe::ProbeScope::AllTraits,
) else { return; };
let mut sugg = ".";
let mut span = start.expr.span.between(end.expr.span);
if span.lo() + BytePos(2) == span.hi() {
// There's no space between the start, the range op and the end, suggest removal which
// will be more noticeable than the replacement of `..` with `.`.
span = span.with_lo(span.lo() + BytePos(1));
sugg = "";
}
err.span_suggestion_verbose(
span,
"you likely meant to write a method call instead of a range",
sugg,
Applicability::MachineApplicable,
);
}
}
26 changes: 26 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rustc_middle::ty::{
TypeVisitable,
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{sym, Ident};
use rustc_span::{Span, Symbol};
use rustc_trait_selection::infer::InferCtxtExt;
Expand Down Expand Up @@ -1259,6 +1260,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
true
}
ExprKind::Lit(Spanned {
node: rustc_ast::LitKind::Int(lit, rustc_ast::LitIntType::Unsuffixed),
span,
}) => {
let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) else { return false; };
if !(snippet.starts_with("0x") || snippet.starts_with("0X")) {
return false;
}
if snippet.len() <= 5 || !snippet.is_char_boundary(snippet.len() - 3) {
return false;
}
let (_, suffix) = snippet.split_at(snippet.len() - 3);
let value = match suffix {
"f32" => (lit - 0xf32) / (16 * 16 * 16),
"f64" => (lit - 0xf64) / (16 * 16 * 16),
_ => return false,
};
err.span_suggestions(
expr.span,
"rewrite this as a decimal floating point literal, or use `as` to turn a hex literal into a float",
[format!("0x{value:X} as {suffix}"), format!("{value}_{suffix}")],
Applicability::MaybeIncorrect,
);
true
}
_ => false,
}
}
Expand Down
Loading