Skip to content
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

Rollup of 5 pull requests #97949

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions compiler/rustc_error_codes/src/error_codes/E0451.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ A struct constructor with private fields was invoked.
Erroneous code example:

```compile_fail,E0451
mod Bar {
mod bar {
pub struct Foo {
pub a: isize,
b: isize,
}
}

let f = Bar::Foo{ a: 0, b: 0 }; // error: field `b` of struct `Bar::Foo`
let f = bar::Foo{ a: 0, b: 0 }; // error: field `b` of struct `bar::Foo`
// is private
```

To fix this error, please ensure that all the fields of the struct are public,
or implement a function for easy instantiation. Examples:

```
mod Bar {
mod bar {
pub struct Foo {
pub a: isize,
pub b: isize, // we set `b` field public
}
}

let f = Bar::Foo{ a: 0, b: 0 }; // ok!
let f = bar::Foo{ a: 0, b: 0 }; // ok!
```

Or:

```
mod Bar {
mod bar {
pub struct Foo {
pub a: isize,
b: isize, // still private
Expand All @@ -44,5 +44,5 @@ mod Bar {
}
}

let f = Bar::Foo::new(); // ok!
let f = bar::Foo::new(); // ok!
```
10 changes: 5 additions & 5 deletions compiler/rustc_error_codes/src/error_codes/E0574.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ expected.
Erroneous code example:

```compile_fail,E0574
mod Mordor {}
mod mordor {}

let sauron = Mordor { x: () }; // error!
let sauron = mordor { x: () }; // error!

enum Jak {
Daxter { i: isize },
Expand All @@ -19,17 +19,17 @@ match eco {
```

In all these errors, a type was expected. For example, in the first error,
we tried to instantiate the `Mordor` module, which is impossible. If you want
we tried to instantiate the `mordor` module, which is impossible. If you want
to instantiate a type inside a module, you can do it as follow:

```
mod Mordor {
mod mordor {
pub struct TheRing {
pub x: usize,
}
}

let sauron = Mordor::TheRing { x: 1 }; // ok!
let sauron = mordor::TheRing { x: 1 }; // ok!
```

In the second error, we tried to bind the `Jak` enum directly, which is not
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_error_codes/src/error_codes/E0577.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ fn main() {}
```

`Sea` is not a module, therefore it is invalid to use it in a visibility path.
To fix this error we need to ensure `Sea` is a module.
To fix this error we need to ensure `sea` is a module.

Please note that the visibility scope can only be applied on ancestors!

```edition2018
pub mod Sea {
pub (in crate::Sea) struct Shark; // ok!
pub mod sea {
pub (in crate::sea) struct Shark; // ok!
}

fn main() {}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_error_codes/src/error_codes/E0603.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ A private item was used outside its scope.
Erroneous code example:

```compile_fail,E0603
mod SomeModule {
mod foo {
const PRIVATE: u32 = 0x_a_bad_1dea_u32; // This const is private, so we
// can't use it outside of the
// `SomeModule` module.
// `foo` module.
}

println!("const value: {}", SomeModule::PRIVATE); // error: constant `PRIVATE`
println!("const value: {}", foo::PRIVATE); // error: constant `PRIVATE`
// is private
```

In order to fix this error, you need to make the item public by using the `pub`
keyword. Example:

```
mod SomeModule {
mod foo {
pub const PRIVATE: u32 = 0x_a_bad_1dea_u32; // We set it public by using the
// `pub` keyword.
}

println!("const value: {}", SomeModule::PRIVATE); // ok!
println!("const value: {}", foo::PRIVATE); // ok!
```
16 changes: 8 additions & 8 deletions compiler/rustc_error_codes/src/error_codes/E0742.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ item.
Erroneous code example:

```compile_fail,E0742,edition2018
pub mod Sea {}
pub mod sea {}

pub (in crate::Sea) struct Shark; // error!
pub (in crate::sea) struct Shark; // error!

fn main() {}
```

To fix this error, we need to move the `Shark` struct inside the `Sea` module:
To fix this error, we need to move the `Shark` struct inside the `sea` module:

```edition2018
pub mod Sea {
pub (in crate::Sea) struct Shark; // ok!
pub mod sea {
pub (in crate::sea) struct Shark; // ok!
}

fn main() {}
Expand All @@ -25,9 +25,9 @@ Of course, you can do it as long as the module you're referring to is an
ancestor:

```edition2018
pub mod Earth {
pub mod Sea {
pub (in crate::Earth) struct Shark; // ok!
pub mod earth {
pub mod sea {
pub (in crate::earth) struct Shark; // ok!
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_expand/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(rustc::potential_query_instability)]
#![feature(array_windows)]
#![feature(associated_type_bounds)]
#![feature(associated_type_defaults)]
#![feature(if_let_guard)]
Expand Down
46 changes: 40 additions & 6 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ pub fn compile_declarative_macro(
features: &Features,
def: &ast::Item,
edition: Edition,
) -> (SyntaxExtension, Vec<Span>) {
) -> (SyntaxExtension, Vec<(usize, Span)>) {
debug!("compile_declarative_macro: {:?}", def);
let mk_syn_ext = |expander| {
SyntaxExtension::new(
Expand Down Expand Up @@ -539,11 +539,22 @@ pub fn compile_declarative_macro(
None => {}
}

// Compute the spans of the macro rules
// We only take the span of the lhs here,
// so that the spans of created warnings are smaller.
let rule_spans = if def.id != DUMMY_NODE_ID {
lhses.iter().map(|lhs| lhs.span()).collect::<Vec<_>>()
// Compute the spans of the macro rules for unused rule linting.
// To avoid warning noise, only consider the rules of this
// macro for the lint, if all rules are valid.
// Also, we are only interested in non-foreign macros.
let rule_spans = if valid && def.id != DUMMY_NODE_ID {
lhses
.iter()
.zip(rhses.iter())
.enumerate()
// If the rhs contains an invocation like compile_error!,
// don't consider the rule for the unused rule lint.
.filter(|(_idx, (_lhs, rhs))| !has_compile_error_macro(rhs))
// We only take the span of the lhs here,
// so that the spans of created warnings are smaller.
.map(|(idx, (lhs, _rhs))| (idx, lhs.span()))
.collect::<Vec<_>>()
} else {
Vec::new()
};
Expand Down Expand Up @@ -651,6 +662,29 @@ fn check_matcher(sess: &ParseSess, def: &ast::Item, matcher: &[mbe::TokenTree])
err == sess.span_diagnostic.err_count()
}

fn has_compile_error_macro(rhs: &mbe::TokenTree) -> bool {
match rhs {
mbe::TokenTree::Delimited(_sp, d) => {
let has_compile_error = d.tts.array_windows::<3>().any(|[ident, bang, args]| {
if let mbe::TokenTree::Token(ident) = ident &&
let TokenKind::Ident(ident, _) = ident.kind &&
ident == sym::compile_error &&
let mbe::TokenTree::Token(bang) = bang &&
let TokenKind::Not = bang.kind &&
let mbe::TokenTree::Delimited(_, del) = args &&
del.delim != Delimiter::Invisible
{
true
} else {
false
}
});
if has_compile_error { true } else { d.tts.iter().any(has_compile_error_macro) }
}
_ => false,
}
}

// `The FirstSets` for a matcher is a mapping from subsequences in the
// matcher to the FIRST set for that subsequence.
//
Expand Down
20 changes: 17 additions & 3 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ pub enum StmtKind<'tcx> {
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Expr<'_>, 104);

#[derive(
Clone,
Debug,
Copy,
PartialEq,
Eq,
Hash,
HashStable,
TyEncodable,
TyDecodable,
TypeFoldable
)]
pub struct LocalVarId(pub hir::HirId);

/// A THIR expression.
#[derive(Clone, Debug, HashStable)]
pub struct Expr<'tcx> {
Expand Down Expand Up @@ -332,15 +346,15 @@ pub enum ExprKind<'tcx> {
},
/// A local variable.
VarRef {
id: hir::HirId,
id: LocalVarId,
},
/// Used to represent upvars mentioned in a closure/generator
UpvarRef {
/// DefId of the closure/generator
closure_def_id: DefId,

/// HirId of the root variable
var_hir_id: hir::HirId,
var_hir_id: LocalVarId,
},
/// A borrow, e.g. `&arg`.
Borrow {
Expand Down Expand Up @@ -596,7 +610,7 @@ pub enum PatKind<'tcx> {
mutability: Mutability,
name: Symbol,
mode: BindingMode,
var: hir::HirId,
var: LocalVarId,
ty: Ty<'tcx>,
subpattern: Option<Pat<'tcx>>,
/// Is this the leftmost occurrence of the binding, i.e., is `var` the
Expand Down
28 changes: 13 additions & 15 deletions compiler/rustc_mir_build/src/build/expr/as_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
use crate::build::expr::category::Category;
use crate::build::ForGuard::{OutsideGuard, RefWithinGuard};
use crate::build::{BlockAnd, BlockAndExtension, Builder};
use rustc_hir::def_id::DefId;
use rustc_hir::HirId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::hir::place::Projection as HirProjection;
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
use rustc_middle::middle::region;
Expand Down Expand Up @@ -57,7 +56,7 @@ pub(crate) enum PlaceBase {
/// figure out that it is captured until all the `Field` projections are applied.
Upvar {
/// HirId of the upvar
var_hir_id: HirId,
var_hir_id: LocalVarId,
/// DefId of the closure
closure_def_id: DefId,
/// The trait closure implements, `Fn`, `FnMut`, `FnOnce`
Expand Down Expand Up @@ -151,12 +150,12 @@ fn is_ancestor_or_same_capture(
/// `ty::MinCaptureList` of the root variable `var_hir_id`.
fn compute_capture_idx<'tcx>(
closure_min_captures: &ty::RootVariableMinCaptureList<'tcx>,
var_hir_id: HirId,
var_hir_id: LocalVarId,
root_var_idx: usize,
) -> usize {
let mut res = 0;
for (var_id, capture_list) in closure_min_captures {
if *var_id == var_hir_id {
if *var_id == var_hir_id.0 {
res += root_var_idx;
break;
} else {
Expand All @@ -176,12 +175,12 @@ fn compute_capture_idx<'tcx>(
/// Returns None, when the ancestor is not found.
fn find_capture_matching_projections<'a, 'tcx>(
typeck_results: &'a ty::TypeckResults<'tcx>,
var_hir_id: HirId,
var_hir_id: LocalVarId,
closure_def_id: DefId,
projections: &[PlaceElem<'tcx>],
) -> Option<(usize, &'a ty::CapturedPlace<'tcx>)> {
let closure_min_captures = typeck_results.closure_min_captures.get(&closure_def_id)?;
let root_variable_min_captures = closure_min_captures.get(&var_hir_id)?;
let root_variable_min_captures = closure_min_captures.get(&var_hir_id.0)?;

let hir_projections = convert_to_hir_projections_and_truncate_for_capture(projections);

Expand Down Expand Up @@ -500,8 +499,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
source_info,
),
ExprKind::UpvarRef { closure_def_id, var_hir_id } => {
let upvar_id = ty::UpvarId::new(var_hir_id, closure_def_id.expect_local());
this.lower_captured_upvar(block, upvar_id)
this.lower_captured_upvar(block, closure_def_id.expect_local(), var_hir_id)
}

ExprKind::VarRef { id } => {
Expand Down Expand Up @@ -627,11 +625,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn lower_captured_upvar(
&mut self,
block: BasicBlock,
upvar_id: ty::UpvarId,
closure_expr_id: LocalDefId,
var_hir_id: LocalVarId,
) -> BlockAnd<PlaceBuilder<'tcx>> {
let closure_ty = self
.typeck_results
.node_type(self.tcx.hir().local_def_id_to_hir_id(upvar_id.closure_expr_id));
let closure_ty =
self.typeck_results.node_type(self.tcx.hir().local_def_id_to_hir_id(closure_expr_id));

let closure_kind = if let ty::Closure(_, closure_substs) = closure_ty.kind() {
self.infcx.closure_kind(closure_substs).unwrap()
Expand All @@ -641,8 +639,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
};

block.and(PlaceBuilder::from(PlaceBase::Upvar {
var_hir_id: upvar_id.var_path.hir_id,
closure_def_id: upvar_id.closure_expr_id.to_def_id(),
var_hir_id,
closure_def_id: closure_expr_id.to_def_id(),
closure_kind,
}))
}
Expand Down
Loading