Skip to content

Commit 17cbdfd

Browse files
committedSep 13, 2022
Auto merge of rust-lang#101777 - matthiaskrgr:rollup-x2dyaa2, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#101266 (translations(rustc_session): migrates rustc_session to use SessionDiagnostic - Final) - rust-lang#101737 (rustdoc: remove no-op CSS `.search-results .result-name > span`) - rust-lang#101752 (Improve Attribute doc methods) - rust-lang#101754 (Fix doc of log function) - rust-lang#101759 (:arrow_up: rust-analyzer) - rust-lang#101765 (Add documentation for TyCtxt::visibility) - rust-lang#101770 (Rustdoc-Json: Don't loose subitems of foreign traits.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents c84083b + f04eee1 commit 17cbdfd

File tree

39 files changed

+699
-181
lines changed

39 files changed

+699
-181
lines changed
 

‎compiler/rustc_ast/src/attr/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,20 @@ impl AttrItem {
232232

233233
impl Attribute {
234234
/// Returns `true` if it is a sugared doc comment (`///` or `//!` for example).
235-
/// So `#[doc = "doc"]` will return `false`.
235+
/// So `#[doc = "doc"]` (which is a doc comment) and `#[doc(...)]` (which is not
236+
/// a doc comment) will return `false`.
236237
pub fn is_doc_comment(&self) -> bool {
237238
match self.kind {
238239
AttrKind::Normal(..) => false,
239240
AttrKind::DocComment(..) => true,
240241
}
241242
}
242243

244+
/// Returns the documentation and its kind if this is a doc comment or a sugared doc comment.
245+
/// * `///doc` returns `Some(("doc", CommentKind::Line))`.
246+
/// * `/** doc */` returns `Some(("doc", CommentKind::Block))`.
247+
/// * `#[doc = "doc"]` returns `Some(("doc", CommentKind::Line))`.
248+
/// * `#[doc(...)]` returns `None`.
243249
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
244250
match self.kind {
245251
AttrKind::DocComment(kind, data) => Some((data, kind)),
@@ -252,6 +258,10 @@ impl Attribute {
252258
}
253259
}
254260

261+
/// Returns the documentation if this is a doc comment or a sugared doc comment.
262+
/// * `///doc` returns `Some("doc")`.
263+
/// * `#[doc = "doc"]` returns `Some("doc")`.
264+
/// * `#[doc(...)]` returns `None`.
255265
pub fn doc_str(&self) -> Option<Symbol> {
256266
match self.kind {
257267
AttrKind::DocComment(.., data) => Some(data),

‎compiler/rustc_error_messages/locales/en-US/session.ftl

+10
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ session_target_invalid_bits_size = {$err}
5656
session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored
5757
5858
session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform
59+
60+
session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions
61+
62+
session_crate_name_does_not_match = `--crate-name` and `#[crate_name]` are required to match, but `{$s}` != `{$name}`
63+
64+
session_crate_name_invalid = crate names cannot start with a `-`, but `{$s}` has a leading hyphen
65+
66+
session_crate_name_empty = crate name must not be empty
67+
68+
session_invalid_character_in_create_name = invalid character `{$character}` in crate name: `{$crate_name}`

‎compiler/rustc_errors/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ rustc_serialize = { path = "../rustc_serialize" }
1313
rustc_span = { path = "../rustc_span" }
1414
rustc_macros = { path = "../rustc_macros" }
1515
rustc_data_structures = { path = "../rustc_data_structures" }
16+
rustc_target = { path = "../rustc_target" }
1617
rustc_hir = { path = "../rustc_hir" }
1718
rustc_lint_defs = { path = "../rustc_lint_defs" }
18-
rustc_target = { path = "../rustc_target" }
1919
unicode-width = "0.1.4"
2020
atty = "0.2"
2121
termcolor = "1.0"

‎compiler/rustc_middle/src/query/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,16 @@ rustc_queries! {
16111611
desc { "looking up late bound vars" }
16121612
}
16131613

1614+
/// Computes the visibility of the provided `def_id`.
1615+
///
1616+
/// If the item from the `def_id` doesn't have a visibility, it will panic. For example
1617+
/// a generic type parameter will panic if you call this method on it:
1618+
///
1619+
/// ```
1620+
/// pub trait Foo<T: Debug> {}
1621+
/// ```
1622+
///
1623+
/// In here, if you call `visibility` on `T`, it'll panic.
16141624
query visibility(def_id: DefId) -> ty::Visibility<DefId> {
16151625
desc { |tcx| "computing visibility of `{}`", tcx.def_path_str(def_id) }
16161626
separate_provide_extern

‎compiler/rustc_session/src/errors.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::num::NonZeroU32;
22

33
use crate::cgu_reuse_tracker::CguReuse;
44
use crate::{self as rustc_session, SessionDiagnostic};
5-
use rustc_errors::{fluent, DiagnosticBuilder, Handler, MultiSpan};
5+
use rustc_errors::{fluent, DiagnosticBuilder, ErrorGuaranteed, Handler, MultiSpan};
66
use rustc_macros::SessionDiagnostic;
77
use rustc_span::{Span, Symbol};
88
use rustc_target::abi::TargetDataLayoutErrors;
@@ -170,3 +170,52 @@ pub struct StackProtectorNotSupportedForTarget<'a> {
170170
pub struct SplitDebugInfoUnstablePlatform {
171171
pub debuginfo: SplitDebuginfo,
172172
}
173+
174+
#[derive(SessionDiagnostic)]
175+
#[diag(session::file_is_not_writeable)]
176+
pub struct FileIsNotWriteable<'a> {
177+
pub file: &'a std::path::Path,
178+
}
179+
180+
#[derive(SessionDiagnostic)]
181+
#[diag(session::crate_name_does_not_match)]
182+
pub struct CrateNameDoesNotMatch<'a> {
183+
#[primary_span]
184+
pub span: Span,
185+
pub s: &'a str,
186+
pub name: Symbol,
187+
}
188+
189+
#[derive(SessionDiagnostic)]
190+
#[diag(session::crate_name_invalid)]
191+
pub struct CrateNameInvalid<'a> {
192+
pub s: &'a str,
193+
}
194+
195+
#[derive(SessionDiagnostic)]
196+
#[diag(session::crate_name_empty)]
197+
pub struct CrateNameEmpty {
198+
#[primary_span]
199+
pub span: Option<Span>,
200+
}
201+
202+
pub struct InvalidCharacterInCrateName<'a> {
203+
pub span: Option<Span>,
204+
pub character: char,
205+
pub crate_name: &'a str,
206+
}
207+
208+
impl crate::SessionDiagnostic<'_> for InvalidCharacterInCrateName<'_> {
209+
fn into_diagnostic(
210+
self,
211+
sess: &Handler,
212+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
213+
let mut diag = sess.struct_err(fluent::session::invalid_character_in_create_name);
214+
if let Some(sp) = self.span {
215+
diag.set_span(sp);
216+
}
217+
diag.set_arg("character", self.character);
218+
diag.set_arg("crate_name", self.crate_name);
219+
diag
220+
}
221+
}

‎compiler/rustc_session/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#![feature(map_many_mut)]
1010
#![recursion_limit = "256"]
1111
#![allow(rustc::potential_query_instability)]
12+
#![deny(rustc::untranslatable_diagnostic)]
13+
#![deny(rustc::diagnostic_outside_of_impl)]
1214

1315
#[macro_use]
1416
extern crate rustc_macros;

‎compiler/rustc_session/src/output.rs

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//! Related to out filenames of compilation (e.g. save analysis, binaries).
22
use crate::config::{CrateType, Input, OutputFilenames, OutputType};
3+
use crate::errors::{
4+
CrateNameDoesNotMatch, CrateNameEmpty, CrateNameInvalid, FileIsNotWriteable,
5+
InvalidCharacterInCrateName,
6+
};
37
use crate::Session;
48
use rustc_ast as ast;
59
use rustc_span::symbol::sym;
@@ -30,11 +34,7 @@ pub fn out_filename(
3034
/// read-only file. We should be consistent.
3135
pub fn check_file_is_writeable(file: &Path, sess: &Session) {
3236
if !is_writeable(file) {
33-
sess.fatal(&format!(
34-
"output file {} is not writeable -- check its \
35-
permissions",
36-
file.display()
37-
));
37+
sess.emit_fatal(FileIsNotWriteable { file });
3838
}
3939
}
4040

@@ -61,11 +61,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
6161
if let Some(ref s) = sess.opts.crate_name {
6262
if let Some((attr, name)) = attr_crate_name {
6363
if name.as_str() != s {
64-
let msg = format!(
65-
"`--crate-name` and `#[crate_name]` are \
66-
required to match, but `{s}` != `{name}`"
67-
);
68-
sess.span_err(attr.span, &msg);
64+
sess.emit_err(CrateNameDoesNotMatch { span: attr.span, s, name });
6965
}
7066
}
7167
return validate(s.clone(), None);
@@ -77,11 +73,7 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
7773
if let Input::File(ref path) = *input {
7874
if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
7975
if s.starts_with('-') {
80-
let msg = format!(
81-
"crate names cannot start with a `-`, but \
82-
`{s}` has a leading hyphen"
83-
);
84-
sess.err(&msg);
76+
sess.emit_err(CrateNameInvalid { s });
8577
} else {
8678
return validate(s.replace('-', "_"), None);
8779
}
@@ -94,15 +86,9 @@ pub fn find_crate_name(sess: &Session, attrs: &[ast::Attribute], input: &Input)
9486
pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
9587
let mut err_count = 0;
9688
{
97-
let mut say = |s: &str| {
98-
match sp {
99-
Some(sp) => sess.span_err(sp, s),
100-
None => sess.err(s),
101-
};
102-
err_count += 1;
103-
};
10489
if s.is_empty() {
105-
say("crate name must not be empty");
90+
err_count += 1;
91+
sess.emit_err(CrateNameEmpty { span: sp });
10692
}
10793
for c in s.chars() {
10894
if c.is_alphanumeric() {
@@ -111,7 +97,8 @@ pub fn validate_crate_name(sess: &Session, s: &str, sp: Option<Span>) {
11197
if c == '_' {
11298
continue;
11399
}
114-
say(&format!("invalid character `{c}` in crate name: `{s}`"));
100+
err_count += 1;
101+
sess.emit_err(InvalidCharacterInCrateName { span: sp, character: c, crate_name: s });
115102
}
116103
}
117104

0 commit comments

Comments
 (0)
Please sign in to comment.