Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
crlf0710 authored Nov 24, 2023
2 parents d39b953 + 4d43a3b commit 05c75ae
Show file tree
Hide file tree
Showing 138 changed files with 4,671 additions and 2,147 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ root = true
[src/*]
end_of_line = lf
insert_final_newline = true
max_line_length = 100

[ci/*.sh]
indent_style = space
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ jobs:
env:
MDBOOK_VERSION: 0.4.21
MDBOOK_LINKCHECK_VERSION: 0.7.6
MDBOOK_MERMAID_VERSION: 0.11.2
MDBOOK_TOC_VERSION: 0.9.0
MDBOOK_MERMAID_VERSION: 0.12.6
MDBOOK_TOC_VERSION: 0.11.2
DEPLOY_DIR: book/html
BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
with:
Expand All @@ -45,7 +46,7 @@ jobs:
if: github.event_name != 'push'
run: |
shopt -s globstar
MAX_LINE_LENGTH=100 bash ci/check_line_lengths.sh src/**/*.md
MAX_LINE_LENGTH=100 bash ci/lengthcheck.sh src/**/*.md
- name: Install latest nightly Rust toolchain
if: steps.mdbook-cache.outputs.cache-hit != 'true'
Expand Down
9 changes: 9 additions & 0 deletions CITATION.cff
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cff-version: 1.2.0
message: If you use this guide, please cite it using these metadata.
title: Rust Compiler Development Guide (rustc-dev-guide)
abstract: A guide to developing the Rust compiler (rustc)
authors:
- name: "The Rust Project Developers"
date-released: "2018-01-16"
license: "MIT OR Apache-2.0"
repository-code: "https://github.com/rust-lang/rustc-dev-guide"
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ rustdocs][rustdocs].
To build a local static HTML site, install [`mdbook`](https://github.com/rust-lang/mdBook) with:

```
> cargo install mdbook mdbook-linkcheck mdbook-toc
> cargo install mdbook mdbook-linkcheck mdbook-toc mdbook-mermaid
```

and execute the following command in the root of the repository:

```
> mdbook build
> mdbook build --open
```

The build files are found in the `book` directory.
The build files are found in the `book/html` directory.

### Link Validations

Expand All @@ -67,20 +67,20 @@ including the `<!-- toc -->` marker at the place where you want the TOC.
### Pre-commit script

We also test that line lengths are less than 100 columns. To test this locally,
you can run `ci/check_line_lengths.sh`.
you can run `ci/lengthcheck.sh`.

You can also set this to run automatically.

On Linux:

```bash
ln -s ../../ci/check_line_lengths.sh .git/hooks/pre-commit
ln -s ../../ci/lengthcheck.sh .git/hooks/pre-commit
```

On Windows:

```powershell
New-Item -Path .git/hooks/pre-commit -ItemType HardLink -Value <absolute_path/to/check_line_lengths.sh>
New-Item -Path .git/hooks/pre-commit -ItemType HardLink -Value $(Resolve-Path ci/lengthcheck.sh)
```

## How to fix toolstate failures
Expand Down Expand Up @@ -118,7 +118,7 @@ git submodule update --remote src/doc/rustc-dev-guide
git add -u
git commit -m "Update rustc-dev-guide"
# Note that you can use -i, which is short for --incremental, in the following command
./x.py test --incremental src/doc/rustc-dev-guide # This is optional and should succeed anyway
./x test --incremental src/doc/rustc-dev-guide # This is optional and should succeed anyway
# Open a PR in rust-lang/rust
```

Expand Down
12 changes: 8 additions & 4 deletions book.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[book]
title = "Guide to Rustc Development"
author = "Rustc developers"
description = "A guide to developing rustc"
title = "Rust Compiler Development Guide"
author = "The Rust Project Developers"
description = "A guide to developing the Rust compiler (rustc)"

[build]
create-missing = false
Expand All @@ -18,6 +18,9 @@ git-repository-url = "https://github.com/rust-lang/rustc-dev-guide"
edit-url-template = "https://github.com/rust-lang/rustc-dev-guide/edit/master/{path}"
additional-js = ["mermaid.min.js", "mermaid-init.js"]

[output.html.search]
use-boolean-and = true

[output.html.fold]
enable = true
level = 0
Expand All @@ -42,5 +45,6 @@ warning-policy = "error"

[output.html.redirect]
"/compiletest.html" = "tests/compiletest.html"
"/diagnostics/sessiondiagnostic.html" = "diagnostics/diagnostic-structs.html"
"/diagnostics/sessiondiagnostic.html" = "diagnostic-structs.html"
"/diagnostics/diagnostic-codes.html" = "error-codes.html"
"/miri.html" = "const-eval/interpret.html"
2 changes: 1 addition & 1 deletion ci/date-check/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn main() {

for (path, dates) in dates_by_file {
println!(
"- [ ] {}",
"- {}",
path.strip_prefix(&root_dir_path).unwrap_or(&path).display(),
);
for (line, date) in dates {
Expand Down
8 changes: 4 additions & 4 deletions ci/check_line_lengths.sh → ci/lengthcheck.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

# Check files for lines that are too long.

if [ "$1" == "--help" ]; then
echo 'Usage:' "[MAX_LINE_LENGTH=n] $0 [file ...]"
exit 1
Expand All @@ -10,8 +12,7 @@ if [ "$MAX_LINE_LENGTH" == "" ]; then
fi

if [ "$1" == "" ]; then
shopt -s globstar
files=( src/**/*.md )
files=( src/*.md src/*/*.md src/*/*/*.md )
else
files=( "$@" )
fi
Expand All @@ -22,7 +23,6 @@ echo "Offending files and lines:"
(( bad_lines = 0 ))
(( inside_block = 0 ))
for file in "${files[@]}"; do
echo "$file"
(( line_no = 0 ))
while IFS="" read -r line || [[ -n "$line" ]] ; do
(( line_no++ ))
Expand All @@ -34,7 +34,7 @@ for file in "${files[@]}"; do
&& ! [[ "$line" =~ " | "|"-|-"|"://"|"]:"|\[\^[^\ ]+\]: ]] \
&& (( "${#line}" > $MAX_LINE_LENGTH )) ; then
(( bad_lines++ ))
echo -e "\t$line_no : $line"
echo -e "\t$file:$line_no : $line"
fi
done < "$file"
done
Expand Down
16 changes: 13 additions & 3 deletions ci/linkcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
set -e
set -o pipefail

set_github_token() {
jq '.config.output.linkcheck."http-headers"."github\\.com" = ["Authorization: Bearer $GITHUB_TOKEN"]'
}

# https://docs.github.com/en/actions/reference/environment-variables
if [ "$GITHUB_EVENT_NAME" = "schedule" ] ; then # running in scheduled job
FLAGS=""
USE_TOKEN=1

echo "Doing full link check."
set -x
elif [ "$GITHUB_EVENT_NAME" = "pull_request" ] ; then # running in PR CI build
if [ -z "$BASE_SHA" ]; then
echo "error: unexpected state: BASE_SHA must be non-empty in CI"
Expand All @@ -17,9 +21,9 @@ elif [ "$GITHUB_EVENT_NAME" = "pull_request" ] ; then # running in PR CI build

CHANGED_FILES=$(git diff --name-only $BASE_SHA... | tr '\n' ' ')
FLAGS="--no-cache -f $CHANGED_FILES"
USE_TOKEN=1

echo "Checking files changed since $BASE_SHA: $CHANGED_FILES"
set -x
else # running locally
COMMIT_RANGE=master...
CHANGED_FILES=$(git diff --name-only $COMMIT_RANGE | tr '\n' ' ')
Expand All @@ -28,4 +32,10 @@ else # running locally
echo "Checking files changed in $COMMIT_RANGE: $CHANGED_FILES"
fi

exec mdbook-linkcheck $FLAGS
echo "exec mdbook-linkcheck $FLAGS"
if [ "$USE_TOKEN" = 1 ]; then
config=$(set_github_token)
exec mdbook-linkcheck $FLAGS <<<"$config"
else
exec mdbook-linkcheck $FLAGS
fi
11 changes: 11 additions & 0 deletions examples/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
For each example to compile, you will need to first run the following:

rustup component add rustc-dev llvm-tools

To create an executable:

rustc rustc-driver-example.rs

To run an executable:

rustup run nightly ./rustc-driver-example
17 changes: 7 additions & 10 deletions examples/rustc-driver-example.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#![feature(rustc_private)]

// NOTE: For the example to compile, you will need to first run the following:
// rustup component add rustc-dev llvm-tools-preview

// version: 1.62.0-nightly (7c4b47696 2022-04-30)

extern crate rustc_driver;
extern crate rustc_error_codes;
extern crate rustc_errors;
extern crate rustc_hash;
Expand Down Expand Up @@ -46,11 +42,10 @@ fn main() {
"#
.into(),
},
input_path: None, // Option<PathBuf>
output_dir: None, // Option<PathBuf>
output_file: None, // Option<PathBuf>
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
diagnostic_output: rustc_session::DiagnosticOutput::Default,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
// This is a callback from the driver that is called when [`ParseSess`] is created.
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
Expand All @@ -68,21 +63,23 @@ fn main() {
// Registry of diagnostics codes.
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
make_codegen_backend: None,
expanded_args: Vec::new(),
ice_file: None,
};
rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
// Parse the program and print the syntax tree.
let parse = queries.parse().unwrap().take();
let parse = queries.parse().unwrap().get_mut().clone();
println!("{parse:?}");
// Analyze the program and inspect the types of definitions.
queries.global_ctxt().unwrap().take().enter(|tcx| {
queries.global_ctxt().unwrap().enter(|tcx| {
for id in tcx.hir().items() {
let hir = tcx.hir();
let item = hir.item(id);
match item.kind {
rustc_hir::ItemKind::Static(_, _, _) | rustc_hir::ItemKind::Fn(_, _, _) => {
let name = item.ident;
let ty = tcx.type_of(hir.local_def_id(item.hir_id()));
let ty = tcx.type_of(item.hir_id().owner.def_id);
println!("{name:?}:\t{ty:?}")
}
_ => (),
Expand Down
16 changes: 5 additions & 11 deletions examples/rustc-driver-getting-diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#![feature(rustc_private)]

// NOTE: For the example to compile, you will need to first run the following:
// rustup component add rustc-dev llvm-tools-preview

// version: 1.62.0-nightly (7c4b47696 2022-04-30)

extern crate rustc_driver;
extern crate rustc_error_codes;
extern crate rustc_errors;
extern crate rustc_hash;
Expand Down Expand Up @@ -65,26 +61,24 @@ fn main() {
"
.into(),
},
// Redirect the diagnostic output of the compiler to a buffer.
diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink(
buffer.clone(),
))),
crate_cfg: rustc_hash::FxHashSet::default(),
crate_check_cfg: CheckCfg::default(),
input_path: None,
output_dir: None,
output_file: None,
file_loader: None,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: rustc_hash::FxHashMap::default(),
parse_sess_created: None,
register_lints: None,
override_queries: None,
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
make_codegen_backend: None,
expanded_args: Vec::new(),
ice_file: None,
};
rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
queries.global_ctxt().unwrap().take().enter(|tcx| {
queries.global_ctxt().unwrap().enter(|tcx| {
// Run the analysis phase on the local crate to trigger the type error.
let _ = tcx.analysis(());
});
Expand Down
18 changes: 7 additions & 11 deletions examples/rustc-driver-interacting-with-the-ast.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#![feature(rustc_private)]

// NOTE: For the example to compile, you will need to first run the following:
// rustup component add rustc-dev llvm-tools-preview

// version: 1.62.0-nightly (7c4b47696 2022-04-30)

extern crate rustc_ast_pretty;
extern crate rustc_driver;
extern crate rustc_error_codes;
extern crate rustc_errors;
extern crate rustc_hash;
Expand Down Expand Up @@ -43,30 +39,30 @@ fn main() {
"#
.to_string(),
},
diagnostic_output: rustc_session::DiagnosticOutput::Default,
crate_cfg: rustc_hash::FxHashSet::default(),
crate_check_cfg: CheckCfg::default(),
input_path: None,
output_dir: None,
output_file: None,
file_loader: None,
locale_resources: rustc_driver::DEFAULT_LOCALE_RESOURCES,
lint_caps: rustc_hash::FxHashMap::default(),
parse_sess_created: None,
register_lints: None,
override_queries: None,
make_codegen_backend: None,
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
expanded_args: Vec::new(),
ice_file: None,
};
rustc_interface::run_compiler(config, |compiler| {
compiler.enter(|queries| {
// TODO: add this to -Z unpretty
let ast_krate = queries.parse().unwrap().take();
let ast_krate = queries.parse().unwrap().get_mut().clone();
for item in ast_krate.items {
println!("{}", item_to_string(&item));
}

// Analyze the crate and inspect the types under the cursor.
queries.global_ctxt().unwrap().take().enter(|tcx| {
queries.global_ctxt().unwrap().enter(|tcx| {
// Every compilation contains a single crate.
let hir_krate = tcx.hir();
// Iterate over the top-level items in the crate, looking for the main function.
Expand All @@ -79,7 +75,7 @@ fn main() {
if let rustc_hir::StmtKind::Local(local) = block.stmts[0].kind {
if let Some(expr) = local.init {
let hir_id = expr.hir_id; // hir_id identifies the string "Hello, world!"
let def_id = tcx.hir().local_def_id(item.hir_id()); // def_id identifies the main function
let def_id = item.hir_id().owner.def_id; // def_id identifies the main function
let ty = tcx.typeck(def_id).node_type(hir_id);
println!("{expr:#?}: {ty:?}");
}
Expand Down
Loading

0 comments on commit 05c75ae

Please sign in to comment.