Skip to content

Commit 0a0e045

Browse files
committed
Auto merge of #111552 - matthiaskrgr:rollup-4nidoti, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #111463 (Better diagnostics for `env!` where variable contains escape) - #111477 (better diagnostics for `impl<..> impl Trait for Type`) - #111534 (rustdoc-json: Add tests for `#![feature(inherent_associated_types)]`) - #111549 ([rustdoc] Convert more GUI tests colors to their original format) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents bc88895 + d1cd127 commit 0a0e045

15 files changed

+296
-100
lines changed

compiler/rustc_builtin_macros/src/env.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,16 @@ pub fn expand_env<'cx>(
6363
Some(exprs) => exprs.into_iter(),
6464
};
6565

66-
let Some((var, _style)) = expr_to_string(cx, exprs.next().unwrap(), "expected string literal") else {
66+
let var_expr = exprs.next().unwrap();
67+
let Some((var, _)) = expr_to_string(cx, var_expr.clone(), "expected string literal") else {
6768
return DummyResult::any(sp);
6869
};
6970

7071
let custom_msg = match exprs.next() {
7172
None => None,
7273
Some(second) => match expr_to_string(cx, second, "expected string literal") {
7374
None => return DummyResult::any(sp),
74-
Some((s, _style)) => Some(s),
75+
Some((s, _)) => Some(s),
7576
},
7677
};
7778

@@ -80,10 +81,15 @@ pub fn expand_env<'cx>(
8081
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
8182
let e = match value {
8283
None => {
84+
// Use the string literal in the code in the diagnostic to avoid confusing diagnostics,
85+
// e.g. when the literal contains escape sequences.
86+
let ast::ExprKind::Lit(ast::token::Lit { kind: ast::token::LitKind::Str, symbol: original_var, ..}) = &var_expr.kind else {
87+
unreachable!("`expr_to_string` ensures this is a string lit")
88+
};
8389
cx.emit_err(errors::EnvNotDefined {
8490
span: sp,
8591
msg: custom_msg,
86-
var,
92+
var: *original_var,
8793
help: custom_msg.is_none().then(|| help_for_missing_env_var(var.as_str())),
8894
});
8995
return DummyResult::any(sp);

compiler/rustc_parse/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ parse_missing_for_in_trait_impl = missing `for` in a trait impl
478478
479479
parse_expected_trait_in_trait_impl_found_type = expected a trait, found type
480480
481+
parse_extra_impl_keyword_in_trait_impl = unexpected `impl` keyword
482+
.suggestion = remove the extra `impl`
483+
.note = this is parsed as an `impl Trait` type, but a trait is expected at this position
484+
485+
481486
parse_non_item_in_item_list = non-item in item list
482487
.suggestion_use_const_not_let = consider using `const` instead of `let` for associated const
483488
.label_list_start = item list starts here

compiler/rustc_parse/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,16 @@ pub(crate) struct ExpectedTraitInTraitImplFoundType {
15191519
pub span: Span,
15201520
}
15211521

1522+
#[derive(Diagnostic)]
1523+
#[diag(parse_extra_impl_keyword_in_trait_impl)]
1524+
pub(crate) struct ExtraImplKeywordInTraitImpl {
1525+
#[primary_span]
1526+
#[suggestion(code = "", applicability = "maybe-incorrect")]
1527+
pub extra_impl_kw: Span,
1528+
#[note]
1529+
pub impl_trait_span: Span,
1530+
}
1531+
15221532
#[derive(Diagnostic)]
15231533
#[diag(parse_bounds_not_allowed_on_trait_aliases)]
15241534
pub(crate) struct BoundsNotAllowedOnTraitAliases {

compiler/rustc_parse/src/parser/item.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,24 @@ impl<'a> Parser<'a> {
603603
let path = match ty_first.kind {
604604
// This notably includes paths passed through `ty` macro fragments (#46438).
605605
TyKind::Path(None, path) => path,
606-
_ => {
607-
self.sess.emit_err(errors::ExpectedTraitInTraitImplFoundType {
608-
span: ty_first.span,
609-
});
606+
other => {
607+
if let TyKind::ImplTrait(_, bounds) = other
608+
&& let [bound] = bounds.as_slice()
609+
{
610+
// Suggest removing extra `impl` keyword:
611+
// `impl<T: Default> impl Default for Wrapper<T>`
612+
// ^^^^^
613+
let extra_impl_kw = ty_first.span.until(bound.span());
614+
self.sess
615+
.emit_err(errors::ExtraImplKeywordInTraitImpl {
616+
extra_impl_kw,
617+
impl_trait_span: ty_first.span
618+
});
619+
} else {
620+
self.sess.emit_err(errors::ExpectedTraitInTraitImplFoundType {
621+
span: ty_first.span,
622+
});
623+
}
610624
err_path(ty_first.span)
611625
}
612626
};
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.16.0
1+
0.16.3

0 commit comments

Comments
 (0)