Skip to content

Rollup of 8 pull requests #118387

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

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b1ad229
Allow setting `rla-*` labels via `rustbot`
tgross35 Aug 10, 2023
8375fe4
Simplify indenting in THIR printing
Mark-Simulacrum Nov 26, 2023
16c164f
Detect and reject malformed repr(Rust) hints
fmease Nov 27, 2023
e65c060
Detect Python-like slicing and suggest how to fix
hkmatsumoto May 3, 2023
61c3e4d
Make tidy test happy
hkmatsumoto May 3, 2023
730d299
Address review feedbacks
hkmatsumoto May 16, 2023
acec70d
Change help message to make some sense in broader context
hkmatsumoto Nov 27, 2023
9e9ca4a
add stable_mir output test
ouz-a Nov 27, 2023
8221f9c
Account for `!` arm in tail `match` expr
estebank Nov 2, 2023
45fc842
rustc_span: Use correct edit distance start length for suggestions
Enselic Nov 27, 2023
765a713
Address unused tuple struct fields in rustdoc
shepmaster Nov 25, 2023
11600ff
Rollup merge of #111133 - hkmatsumoto:handle-python-slicing, r=TaKO8Ki
GuillaumeGomez Nov 27, 2023
1d1bc24
Rollup merge of #114708 - tgross35:tgross35-patch-1, r=Mark-Simulacrum
GuillaumeGomez Nov 27, 2023
e1c8ada
Rollup merge of #117526 - estebank:issue-24157, r=b-naber
GuillaumeGomez Nov 27, 2023
83df769
Rollup merge of #118341 - Mark-Simulacrum:shrink-thir-print, r=compil…
GuillaumeGomez Nov 27, 2023
4c9a2ab
Rollup merge of #118366 - fmease:detect-reject-malformed-rust-repr, r…
GuillaumeGomez Nov 27, 2023
52437c7
Rollup merge of #118375 - ouz-a:add_emit_stable_mir_tests, r=compiler…
GuillaumeGomez Nov 27, 2023
73070b1
Rollup merge of #118381 - Enselic:edit-dist-len, r=WaffleLapkin
GuillaumeGomez Nov 27, 2023
bec9f90
Rollup merge of #118384 - shepmaster:unused-tuple-struct-field-cleanu…
GuillaumeGomez Nov 27, 2023
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
5 changes: 5 additions & 0 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,11 @@ impl Token {
)
}

/// Returns `true` if the token is the integer literal.
pub fn is_integer_lit(&self) -> bool {
matches!(self.kind, Literal(Lit { kind: LitKind::Integer, .. }))
}

/// Returns `true` if the token is a non-raw identifier for which `pred` holds.
pub fn is_non_raw_ident_where(&self, pred: impl FnOnce(Ident) -> bool) -> bool {
match self.ident() {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
Ok(literal) => acc.push(ReprPacked(literal)),
Err(message) => literal_error = Some(message),
};
} else if matches!(name, sym::C | sym::simd | sym::transparent)
} else if matches!(name, sym::Rust | sym::C | sym::simd | sym::transparent)
|| int_type_of_word(name).is_some()
{
recognised = true;
Expand Down Expand Up @@ -1018,7 +1018,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
});
} else if matches!(
meta_item.name_or_empty(),
sym::C | sym::simd | sym::transparent
sym::Rust | sym::C | sym::simd | sym::transparent
) || int_type_of_word(meta_item.name_or_empty()).is_some()
{
recognised = true;
Expand All @@ -1043,7 +1043,7 @@ pub fn parse_repr_attr(sess: &Session, attr: &Attribute) -> Vec<ReprAttr> {
);
} else if matches!(
meta_item.name_or_empty(),
sym::C | sym::simd | sym::transparent
sym::Rust | sym::C | sym::simd | sym::transparent
) || int_type_of_word(meta_item.name_or_empty()).is_some()
{
recognised = true;
Expand Down
36 changes: 35 additions & 1 deletion compiler/rustc_hir_typeck/src/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&cause,
Some(arm.body),
arm_ty,
|err| self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm),
|err| {
self.explain_never_type_coerced_to_unit(err, arm, arm_ty, prior_arm, expr);
},
false,
);

Expand Down Expand Up @@ -177,6 +179,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
coercion.complete(self)
}

fn explain_never_type_coerced_to_unit(
&self,
err: &mut Diagnostic,
arm: &hir::Arm<'tcx>,
arm_ty: Ty<'tcx>,
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
expr: &hir::Expr<'tcx>,
) {
if let hir::ExprKind::Block(block, _) = arm.body.kind
&& let Some(expr) = block.expr
&& let arm_tail_ty = self.node_ty(expr.hir_id)
&& arm_tail_ty.is_never()
&& !arm_ty.is_never()
{
err.span_label(
expr.span,
format!(
"this expression is of type `!`, but it is coerced to `{arm_ty}` due to its \
surrounding expression",
),
);
self.suggest_mismatched_types_on_tail(
err,
expr,
arm_ty,
prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
expr.hir_id,
);
}
self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
}

fn suggest_removing_semicolon_for_coerce(
&self,
diag: &mut Diagnostic,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
// label pointing out the cause for the type coercion will be wrong
// as prior return coercions would not be relevant (#57664).
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) {
fcx.suggest_missing_semicolon(&mut err, expr, expected, false);
let pointing_at_return_type =
fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
if let (Some(cond_expr), true, false) = (
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
coerce.coerce_forced_unit(
self,
&cause,
|err| {
self.suggest_mismatched_types_on_tail(err, expr, ty, e_ty, target_id);
|mut err| {
self.suggest_missing_semicolon(&mut err, expr, e_ty, false);
self.suggest_mismatched_types_on_tail(
&mut err, expr, ty, e_ty, target_id,
);
let error = Some(Sorts(ExpectedFound { expected: ty, found: e_ty }));
self.annotate_loop_expected_due_to_inference(err, expr, error);
if let Some(val) = ty_kind_suggestion(ty) {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
blk_id: hir::HirId,
) -> bool {
let expr = expr.peel_drop_temps();
self.suggest_missing_semicolon(err, expr, expected, false);
let mut pointing_at_return_type = false;
if let hir::ExprKind::Break(..) = expr.kind {
// `break` type mismatches provide better context for tail `loop` expressions.
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_mir_build/src/thir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const INDENT: &str = " ";

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

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

fn indent(&mut self, level: usize) {
for _ in 0..level {
self.fmt.push_str(INDENT);
}
}

fn print(&mut self) {
print_indented!(self, "params: [", 0);
for param in self.thir.params.iter() {
Expand Down
39 changes: 28 additions & 11 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,20 +567,37 @@ impl<'a> Parser<'a> {
snapshot.recover_diff_marker();
}
if self.token == token::Colon {
// if next token is following a colon, it's likely a path
// and we can suggest a path separator
self.bump();
if self.token.span.lo() == self.prev_token.span.hi() {
// if a previous and next token of the current one is
// integer literal (e.g. `1:42`), it's likely a range
// expression for Pythonistas and we can suggest so.
if self.prev_token.is_integer_lit()
&& self.may_recover()
&& self.look_ahead(1, |token| token.is_integer_lit())
{
// FIXME(hkmatsumoto): Might be better to trigger
// this only when parsing an index expression.
err.span_suggestion_verbose(
self.prev_token.span,
"maybe write a path separator here",
"::",
self.token.span,
"you might have meant a range expression",
"..",
Applicability::MaybeIncorrect,
);
}
if self.sess.unstable_features.is_nightly_build() {
// FIXME(Nilstrieb): Remove this again after a few months.
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
} else {
// if next token is following a colon, it's likely a path
// and we can suggest a path separator
self.bump();
if self.token.span.lo() == self.prev_token.span.hi() {
err.span_suggestion_verbose(
self.prev_token.span,
"maybe write a path separator here",
"::",
Applicability::MaybeIncorrect,
);
}
if self.sess.unstable_features.is_nightly_build() {
// FIXME(Nilstrieb): Remove this again after a few months.
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_span/src/edit_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ fn find_best_match_for_name_impl(
return Some(*c);
}

let mut dist = dist.unwrap_or_else(|| cmp::max(lookup.len(), 3) / 3);
// `fn edit_distance()` use `chars()` to calculate edit distance, so we must
// also use `chars()` (and not `str::len()`) to calculate length here.
let lookup_len = lookup.chars().count();

let mut dist = dist.unwrap_or_else(|| cmp::max(lookup_len, 3) / 3);
let mut best = None;
// store the candidates with the same distance, only for `use_substring_score` current.
let mut next_candidates = vec![];
Expand Down
7 changes: 2 additions & 5 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,11 +1821,8 @@ fn maybe_expand_private_type_alias<'tcx>(
}
_ => None,
});
if let Some(ct) = const_ {
args.insert(
param.def_id.to_def_id(),
SubstParam::Constant(clean_const(ct, cx)),
);
if let Some(_) = const_ {
args.insert(param.def_id.to_def_id(), SubstParam::Constant);
}
// FIXME(const_generics_defaults)
indices.consts += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,7 +2546,7 @@ pub(crate) enum TypeBindingKind {
pub(crate) enum SubstParam {
Type(Type),
Lifetime(Lifetime),
Constant(Constant),
Constant,
}

impl SubstParam {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/tidy/src/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
const ENTRY_LIMIT: usize = 900;
// FIXME: The following limits should be reduced eventually.
const ISSUES_ENTRY_LIMIT: usize = 1852;
const ROOT_ENTRY_LIMIT: usize = 867;
const ROOT_ENTRY_LIMIT: usize = 868;

const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
"rs", // test source files
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/match/match-tail-expr-never-type-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fn never() -> ! {
loop {}
}

fn bar(a: bool) {
match a {
true => 1,
false => {
never() //~ ERROR `match` arms have incompatible types
}
}
}
fn main() {
bar(true);
bar(false);
}
21 changes: 21 additions & 0 deletions tests/ui/match/match-tail-expr-never-type-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0308]: `match` arms have incompatible types
--> $DIR/match-tail-expr-never-type-error.rs:9:13
|
LL | fn bar(a: bool) {
| - help: try adding a return type: `-> i32`
LL | / match a {
LL | | true => 1,
| | - this is found to be of type `{integer}`
LL | | false => {
LL | | never()
| | ^^^^^^^
| | |
| | expected integer, found `()`
| | this expression is of type `!`, but it is coerced to `()` due to its surrounding expression
LL | | }
LL | | }
| |_____- `match` arms have incompatible types

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ struct S3;
//~^ ERROR: incorrect `repr(align)` attribute format
struct S4;

// Regression test for issue #118334:
#[repr(Rust(u8))]
//~^ ERROR: invalid representation hint
#[repr(Rust(0))]
//~^ ERROR: invalid representation hint
#[repr(Rust = 0)]
//~^ ERROR: invalid representation hint
struct S5;

#[repr(i8())]
//~^ ERROR: invalid representation hint
enum E1 { A, B }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
error[E0552]: incorrect `repr(packed)` attribute format: `packed` takes exactly one parenthesized argument, or no parentheses at all
--> $DIR/issue-83921-ice.rs:6:8
--> $DIR/malformed-repr-hints.rs:6:8
|
LL | #[repr(packed())]
| ^^^^^^^^

error[E0589]: invalid `repr(align)` attribute: `align` needs an argument
--> $DIR/issue-83921-ice.rs:10:8
--> $DIR/malformed-repr-hints.rs:10:8
|
LL | #[repr(align)]
| ^^^^^ help: supply an argument here: `align(...)`

error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
--> $DIR/issue-83921-ice.rs:14:8
--> $DIR/malformed-repr-hints.rs:14:8
|
LL | #[repr(align(2, 4))]
| ^^^^^^^^^^^

error[E0693]: incorrect `repr(align)` attribute format: `align` takes exactly one argument in parentheses
--> $DIR/issue-83921-ice.rs:18:8
--> $DIR/malformed-repr-hints.rs:18:8
|
LL | #[repr(align())]
| ^^^^^^^

error[E0552]: invalid representation hint: `Rust` does not take a parenthesized argument list
--> $DIR/malformed-repr-hints.rs:23:8
|
LL | #[repr(Rust(u8))]
| ^^^^^^^^

error[E0552]: invalid representation hint: `Rust` does not take a parenthesized argument list
--> $DIR/malformed-repr-hints.rs:25:8
|
LL | #[repr(Rust(0))]
| ^^^^^^^

error[E0552]: invalid representation hint: `Rust` does not take a value
--> $DIR/malformed-repr-hints.rs:27:8
|
LL | #[repr(Rust = 0)]
| ^^^^^^^^

error[E0552]: invalid representation hint: `i8` does not take a parenthesized argument list
--> $DIR/issue-83921-ice.rs:22:8
--> $DIR/malformed-repr-hints.rs:31:8
|
LL | #[repr(i8())]
| ^^^^

error[E0552]: invalid representation hint: `u32` does not take a parenthesized argument list
--> $DIR/issue-83921-ice.rs:26:8
--> $DIR/malformed-repr-hints.rs:35:8
|
LL | #[repr(u32(42))]
| ^^^^^^^

error[E0552]: invalid representation hint: `i64` does not take a value
--> $DIR/issue-83921-ice.rs:30:8
--> $DIR/malformed-repr-hints.rs:39:8
|
LL | #[repr(i64 = 2)]
| ^^^^^^^

error: aborting due to 7 previous errors
error: aborting due to 10 previous errors

Some errors have detailed explanations: E0552, E0589, E0693.
For more information about an error, try `rustc --explain E0552`.
14 changes: 14 additions & 0 deletions tests/ui/stable-mir-print/basic_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// compile-flags: -Z unpretty=stable-mir
// check-pass

fn foo(i:i32) -> i32 {
i + 1
}

fn bar(vec: &mut Vec<i32>) -> Vec<i32> {
let mut new_vec = vec.clone();
new_vec.push(1);
new_vec
}

fn main(){}
Loading