Skip to content

Commit 2be7cb0

Browse files
committed
Invert dependency between rustc_error_messages and rustc_ast*.
`rustc_error_messages` currently depends on `rustc_ast`/`rustc_ast_pretty`. This is odd, because `rustc_error_messages` feels like a very low-level module but `rustc_ast`/`rustc_ast_pretty` do not. The reason is that a few AST types impl `IntoDiagArg` via pretty-printing. `rustc_error_messages` can define `IntoDiagArg` and then impl it for the AST types. But if we invert the dependency we hit a problem with the orphan rule: `rustc_ast` must impl `IntoDiagArg` for the AST types, but that requires calling pretty-printing code which is in `rustc_ast_pretty`, a downstream crate. This commit avoids this problem by just removing the `IntoDiagArg` impls for these AST types. There aren't that many of them, and we can just use `String` in the relevant error structs and use the pretty printer in the downstream crates that construct the error structs. There are plenty of existing examples where `String` is used in error structs. There is now no dependency between `rustc_ast*` and `rustc_error_messages`.
1 parent a021a77 commit 2be7cb0

16 files changed

Lines changed: 64 additions & 108 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3853,8 +3853,6 @@ dependencies = [
38533853
"icu_list",
38543854
"icu_locale",
38553855
"intl-memoizer",
3856-
"rustc_ast",
3857-
"rustc_ast_pretty",
38583856
"rustc_baked_icu_data",
38593857
"rustc_data_structures",
38603858
"rustc_macros",

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,8 @@ fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericPara
11171117
dcx.emit_err(errors::OutOfOrderParams {
11181118
spans: spans.clone(),
11191119
sugg_span: span,
1120-
param_ord,
1121-
max_param,
1120+
param_ord: param_ord.to_string(),
1121+
max_param: max_param.to_string(),
11221122
ordered_params: &ordered_params,
11231123
});
11241124
}

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Errors emitted by ast_passes.
22
33
use rustc_abi::ExternAbi;
4-
use rustc_ast::ParamKindOrd;
54
use rustc_errors::codes::*;
65
use rustc_errors::{Applicability, Diag, EmissionGuarantee, Subdiagnostic};
76
use rustc_macros::{Diagnostic, Subdiagnostic};
@@ -632,8 +631,8 @@ pub(crate) struct OutOfOrderParams<'a> {
632631
applicability = "machine-applicable"
633632
)]
634633
pub sugg_span: Span,
635-
pub param_ord: &'a ParamKindOrd,
636-
pub max_param: &'a ParamKindOrd,
634+
pub param_ord: String,
635+
pub max_param: String,
637636
pub ordered_params: &'a str,
638637
}
639638

compiler/rustc_builtin_macros/src/env.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::env::VarError;
99
use rustc_ast::token::{self, LitKind};
1010
use rustc_ast::tokenstream::TokenStream;
1111
use rustc_ast::{ExprKind, GenericArg, Mutability};
12+
use rustc_ast_pretty::pprust;
1213
use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacEager, MacroExpanderResult};
1314
use rustc_span::edit_distance::edit_distance;
1415
use rustc_span::{Ident, Span, Symbol, kw, sym};
@@ -158,13 +159,13 @@ pub(crate) fn expand_env<'cx>(
158159
cx.dcx().emit_err(errors::EnvNotDefined::CargoEnvVar {
159160
span,
160161
var: *symbol,
161-
var_expr: &var_expr,
162+
var_expr: pprust::expr_to_string(&var_expr),
162163
})
163164
} else {
164165
cx.dcx().emit_err(errors::EnvNotDefined::CustomEnvVar {
165166
span,
166167
var: *symbol,
167-
var_expr: &var_expr,
168+
var_expr: pprust::expr_to_string(&var_expr),
168169
})
169170
}
170171
}

compiler/rustc_builtin_macros/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for EnvNotDefinedWithUserMessag
553553
}
554554

555555
#[derive(Diagnostic)]
556-
pub(crate) enum EnvNotDefined<'a> {
556+
pub(crate) enum EnvNotDefined {
557557
#[diag("environment variable `{$var}` not defined at compile time")]
558558
#[help("`{$var}` may not be available for the current Cargo target")]
559559
#[help(
@@ -563,7 +563,7 @@ pub(crate) enum EnvNotDefined<'a> {
563563
#[primary_span]
564564
span: Span,
565565
var: Symbol,
566-
var_expr: &'a rustc_ast::Expr,
566+
var_expr: String,
567567
},
568568
#[diag("environment variable `{$var}` not defined at compile time")]
569569
#[help("there is a similar Cargo environment variable: `{$suggested_var}`")]
@@ -579,7 +579,7 @@ pub(crate) enum EnvNotDefined<'a> {
579579
#[primary_span]
580580
span: Span,
581581
var: Symbol,
582-
var_expr: &'a rustc_ast::Expr,
582+
var_expr: String,
583583
},
584584
}
585585

compiler/rustc_codegen_gcc/src/intrinsic/simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
828828
return_error!(InvalidMonomorphization::FloatingPointVector {
829829
span,
830830
name,
831-
f_ty: *f,
831+
f_ty: f.name_str().to_string(),
832832
in_ty
833833
});
834834
}

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use rustc_errors::{
1313
Level, msg,
1414
};
1515
use rustc_macros::{Diagnostic, Subdiagnostic};
16+
use rustc_middle::ty::Ty;
1617
use rustc_middle::ty::layout::LayoutError;
17-
use rustc_middle::ty::{FloatTy, Ty};
1818
use rustc_span::{Span, Symbol};
1919

2020
use crate::assert_module_sources::CguReuse;
@@ -748,7 +748,7 @@ pub enum InvalidMonomorphization<'tcx> {
748748
#[primary_span]
749749
span: Span,
750750
name: Symbol,
751-
f_ty: FloatTy,
751+
f_ty: String,
752752
in_ty: Ty<'tcx>,
753753
},
754754

compiler/rustc_error_messages/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ fluent-bundle = "0.16"
99
icu_list = { version = "2.0", default-features = false, features = ["alloc"] }
1010
icu_locale = { version = "2.0", default-features = false }
1111
intl-memoizer = "0.5.1"
12-
rustc_ast = { path = "../rustc_ast" }
13-
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1412
rustc_baked_icu_data = { path = "../rustc_baked_icu_data" }
1513
rustc_data_structures = { path = "../rustc_data_structures" }
1614
rustc_macros = { path = "../rustc_macros" }

compiler/rustc_error_messages/src/diagnostic_impls.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use std::num::ParseIntError;
55
use std::path::{Path, PathBuf};
66
use std::process::ExitStatus;
77

8-
use rustc_ast as ast;
9-
use rustc_ast_pretty::pprust;
108
use rustc_span::edition::Edition;
119

1210
use crate::{DiagArgValue, IntoDiagArg};
@@ -69,7 +67,6 @@ macro_rules! into_diag_arg_for_number {
6967
}
7068

7169
into_diag_arg_using_display!(
72-
ast::ParamKindOrd,
7370
std::io::Error,
7471
Box<dyn std::error::Error>,
7572
std::num::NonZero<u32>,
@@ -142,30 +139,6 @@ impl IntoDiagArg for PathBuf {
142139
}
143140
}
144141

145-
impl IntoDiagArg for ast::Expr {
146-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
147-
DiagArgValue::Str(Cow::Owned(pprust::expr_to_string(&self)))
148-
}
149-
}
150-
151-
impl IntoDiagArg for ast::Path {
152-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
153-
DiagArgValue::Str(Cow::Owned(pprust::path_to_string(&self)))
154-
}
155-
}
156-
157-
impl IntoDiagArg for ast::token::Token {
158-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
159-
DiagArgValue::Str(pprust::token_to_string(&self))
160-
}
161-
}
162-
163-
impl IntoDiagArg for ast::token::TokenKind {
164-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
165-
DiagArgValue::Str(pprust::token_kind_to_string(&self))
166-
}
167-
}
168-
169142
impl IntoDiagArg for std::ffi::CString {
170143
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
171144
DiagArgValue::Str(Cow::Owned(self.to_string_lossy().into_owned()))
@@ -178,28 +151,8 @@ impl IntoDiagArg for rustc_data_structures::small_c_str::SmallCStr {
178151
}
179152
}
180153

181-
impl IntoDiagArg for ast::Visibility {
182-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
183-
let s = pprust::vis_to_string(&self);
184-
let s = s.trim_end().to_string();
185-
DiagArgValue::Str(Cow::Owned(s))
186-
}
187-
}
188-
189154
impl IntoDiagArg for Backtrace {
190155
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
191156
DiagArgValue::Str(Cow::from(self.to_string()))
192157
}
193158
}
194-
195-
impl IntoDiagArg for ast::util::parser::ExprPrecedence {
196-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
197-
DiagArgValue::Number(self as i32)
198-
}
199-
}
200-
201-
impl IntoDiagArg for ast::FloatTy {
202-
fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
203-
DiagArgValue::Str(Cow::Borrowed(self.name_str()))
204-
}
205-
}

compiler/rustc_expand/src/errors.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::borrow::Cow;
22

3-
use rustc_ast::ast;
43
use rustc_errors::codes::*;
54
use rustc_hir::limit::Limit;
65
use rustc_macros::{Diagnostic, Subdiagnostic};
@@ -230,7 +229,7 @@ pub(crate) struct WrongFragmentKind<'a> {
230229
#[primary_span]
231230
pub span: Span,
232231
pub kind: &'a str,
233-
pub name: &'a ast::Path,
232+
pub name: String,
234233
}
235234

236235
#[derive(Diagnostic)]
@@ -249,7 +248,7 @@ pub(crate) struct IncompleteParse<'a> {
249248
pub descr: String,
250249
#[label("caused by the macro expansion here")]
251250
pub label_span: Span,
252-
pub macro_path: &'a ast::Path,
251+
pub macro_path: String,
253252
pub kind_name: &'a str,
254253
#[note("macros cannot expand to match arms")]
255254
pub expands_to_match_arm: bool,

0 commit comments

Comments
 (0)