Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5466718

Browse files
committed
Auto merge of rust-lang#118387 - GuillaumeGomez:rollup-hbkx6nc, r=GuillaumeGomez
Rollup of 8 pull requests Successful merges: - rust-lang#111133 (Detect Python-like slicing and suggest how to fix) - rust-lang#114708 (Allow setting `rla` labels via `rustbot`) - rust-lang#117526 (Account for `!` arm in tail `match` expr) - rust-lang#118341 (Simplify indenting in THIR printing) - rust-lang#118366 (Detect and reject malformed `repr(Rust)` hints) - rust-lang#118375 (Add -Zunpretty=stable-mir output test) - rust-lang#118381 (rustc_span: Use correct edit distance start length for suggestions) - rust-lang#118384 (Address unused tuple struct fields in rustdoc) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 49b3924 + bec9f90 commit 5466718

File tree

23 files changed

+440
-36
lines changed

23 files changed

+440
-36
lines changed

compiler/rustc_ast/src/token.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,11 @@ impl Token {
756756
)
757757
}
758758

759+
/// Returns `true` if the token is the integer literal.
760+
pub fn is_integer_lit(&self) -> bool {
761+
matches!(self.kind, Literal(Lit { kind: LitKind::Integer, .. }))
762+
}
763+
759764
/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
760765
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
761766
match self.ident() {

compiler/rustc_attr/src/builtin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
985985
Ok(literal) => acc.push(ReprPacked(literal)),
986986
Err(message) => literal_error = Some(message),
987987
};
988-
} else if matches!(name, sym::C | sym::simd | sym::transparent)
988+
} else if matches!(name, sym::Rust | sym::C | sym::simd | sym::transparent)
989989
|| int_type_of_word(name).is_some()
990990
{
991991
recognised = true;
@@ -1018,7 +1018,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10181018
});
10191019
} else if matches!(
10201020
meta_item.name_or_empty(),
1021-
sym::C | sym::simd | sym::transparent
1021+
sym::Rust | sym::C | sym::simd | sym::transparent
10221022
) || int_type_of_word(meta_item.name_or_empty()).is_some()
10231023
{
10241024
recognised = true;
@@ -1043,7 +1043,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
10431043
);
10441044
} else if matches!(
10451045
meta_item.name_or_empty(),
1046-
sym::C | sym::simd | sym::transparent
1046+
sym::Rust | sym::C | sym::simd | sym::transparent
10471047
) || int_type_of_word(meta_item.name_or_empty()).is_some()
10481048
{
10491049
recognised = true;

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139139
&cause,
140140
Some(arm.body),
141141
arm_ty,
142-
|err| self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm),
142+
|err| {
143+
self.explain_never_type_coerced_to_unit(err, arm, arm_ty, prior_arm, expr);
144+
},
143145
false,
144146
);
145147

@@ -177,6 +179,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
177179
coercion.complete(self)
178180
}
179181

182+
fn explain_never_type_coerced_to_unit(
183+
&self,
184+
err: &mut Diagnostic,
185+
arm: &hir::Arm<'tcx>,
186+
arm_ty: Ty<'tcx>,
187+
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
188+
expr: &hir::Expr<'tcx>,
189+
) {
190+
if let hir::ExprKind::Block(block, _) = arm.body.kind
191+
&& let Some(expr) = block.expr
192+
&& let arm_tail_ty = self.node_ty(expr.hir_id)
193+
&& arm_tail_ty.is_never()
194+
&& !arm_ty.is_never()
195+
{
196+
err.span_label(
197+
expr.span,
198+
format!(
199+
"this expression is of type `!`, but it is coerced to `{arm_ty}` due to its \
200+
surrounding expression",
201+
),
202+
);
203+
self.suggest_mismatched_types_on_tail(
204+
err,
205+
expr,
206+
arm_ty,
207+
prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
208+
expr.hir_id,
209+
);
210+
}
211+
self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
212+
}
213+
180214
fn suggest_removing_semicolon_for_coerce(
181215
&self,
182216
diag: &mut Diagnostic,

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17151715
// label pointing out the cause for the type coercion will be wrong
17161716
// as prior return coercions would not be relevant (#57664).
17171717
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) {
1718+
fcx.suggest_missing_semicolon(&mut err, expr, expected, false);
17181719
let pointing_at_return_type =
17191720
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
17201721
if let (Some(cond_expr), true, false) = (

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
663663
coerce.coerce_forced_unit(
664664
self,
665665
&cause,
666-
|err| {
667-
self.suggest_mismatched_types_on_tail(err, expr, ty, e_ty, target_id);
666+
|mut err| {
667+
self.suggest_missing_semicolon(&mut err, expr, e_ty, false);
668+
self.suggest_mismatched_types_on_tail(
669+
&mut err, expr, ty, e_ty, target_id,
670+
);
668671
let error = Some(Sorts(ExpectedFound { expected: ty, found: e_ty }));
669672
self.annotate_loop_expected_due_to_inference(err, expr, error);
670673
if let Some(val) = ty_kind_suggestion(ty) {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7272
blk_id: hir::HirId,
7373
) -> bool {
7474
let expr = expr.peel_drop_temps();
75-
self.suggest_missing_semicolon(err, expr, expected, false);
7675
let mut pointing_at_return_type = false;
7776
if let hir::ExprKind::Break(..) = expr.kind {
7877
// `break` type mismatches provide better context for tail `loop` expressions.

compiler/rustc_mir_build/src/thir/print.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const INDENT: &str = " ";
3131

3232
macro_rules! print_indented {
3333
($writer:ident, $s:expr, $indent_lvl:expr) => {
34-
let indent = (0..$indent_lvl).map(|_| INDENT).collect::<Vec<_>>().concat();
35-
writeln!($writer, "{}{}", indent, $s).expect("unable to write to ThirPrinter");
34+
$writer.indent($indent_lvl);
35+
writeln!($writer, "{}", $s).expect("unable to write to ThirPrinter");
3636
};
3737
}
3838

@@ -48,6 +48,12 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
4848
Self { thir, fmt: String::new() }
4949
}
5050

51+
fn indent(&mut self, level: usize) {
52+
for _ in 0..level {
53+
self.fmt.push_str(INDENT);
54+
}
55+
}
56+
5157
fn print(&mut self) {
5258
print_indented!(self, "params: [", 0);
5359
for param in self.thir.params.iter() {

compiler/rustc_parse/src/parser/stmt.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -567,20 +567,37 @@ impl<'a> Parser<'a> {
567567
snapshot.recover_diff_marker();
568568
}
569569
if self.token == token::Colon {
570-
// if next token is following a colon, it's likely a path
571-
// and we can suggest a path separator
572-
self.bump();
573-
if self.token.span.lo() == self.prev_token.span.hi() {
570+
// if a previous and next token of the current one is
571+
// integer literal (e.g. `1:42`), it's likely a range
572+
// expression for Pythonistas and we can suggest so.
573+
if self.prev_token.is_integer_lit()
574+
&& self.may_recover()
575+
&& self.look_ahead(1, |token| token.is_integer_lit())
576+
{
577+
// FIXME(hkmatsumoto): Might be better to trigger
578+
// this only when parsing an index expression.
574579
err.span_suggestion_verbose(
575-
self.prev_token.span,
576-
"maybe write a path separator here",
577-
"::",
580+
self.token.span,
581+
"you might have meant a range expression",
582+
"..",
578583
Applicability::MaybeIncorrect,
579584
);
580-
}
581-
if self.sess.unstable_features.is_nightly_build() {
582-
// FIXME(Nilstrieb): Remove this again after a few months.
583-
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
585+
} else {
586+
// if next token is following a colon, it's likely a path
587+
// and we can suggest a path separator
588+
self.bump();
589+
if self.token.span.lo() == self.prev_token.span.hi() {
590+
err.span_suggestion_verbose(
591+
self.prev_token.span,
592+
"maybe write a path separator here",
593+
"::",
594+
Applicability::MaybeIncorrect,
595+
);
596+
}
597+
if self.sess.unstable_features.is_nightly_build() {
598+
// FIXME(Nilstrieb): Remove this again after a few months.
599+
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
600+
}
584601
}
585602
}
586603

compiler/rustc_span/src/edit_distance.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ fn find_best_match_for_name_impl(
188188
return Some(*c);
189189
}
190190

191-
let mut dist = dist.unwrap_or_else(|| cmp::max(lookup.len(), 3) / 3);
191+
// `fn edit_distance()` use `chars()` to calculate edit distance, so we must
192+
// also use `chars()` (and not `str::len()`) to calculate length here.
193+
let lookup_len = lookup.chars().count();
194+
195+
let mut dist = dist.unwrap_or_else(|| cmp::max(lookup_len, 3) / 3);
192196
let mut best = None;
193197
// store the candidates with the same distance, only for `use_substring_score` current.
194198
let mut next_candidates = vec![];

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,11 +1821,8 @@ fn maybe_expand_private_type_alias<'tcx>(
18211821
}
18221822
_ => None,
18231823
});
1824-
if let Some(ct) = const_ {
1825-
args.insert(
1826-
param.def_id.to_def_id(),
1827-
SubstParam::Constant(clean_const(ct, cx)),
1828-
);
1824+
if let Some(_) = const_ {
1825+
args.insert(param.def_id.to_def_id(), SubstParam::Constant);
18291826
}
18301827
// FIXME(const_generics_defaults)
18311828
indices.consts += 1;

0 commit comments

Comments
 (0)