Skip to content

Commit 0fda505

Browse files
committed
Auto merge of #159791 - cuviper:beta-next, r=cuviper
[beta] backports - Fix string indexing in diagnostic format strings #159470 - Resolver: Record at least 1 ambiguous trait if main decl is not a trait. #159599 - Set the rustc lib path for unstable-book-gen #159613 r? me
2 parents c666fcb + 3eda017 commit 0fda505

10 files changed

Lines changed: 137 additions & 17 deletions

File tree

‎compiler/rustc_lint_defs/src/builtin.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4560,7 +4560,7 @@ declare_lint! {
45604560
Warn,
45614561
"detects uses of ambiguously glob imported traits",
45624562
@future_incompatible = FutureIncompatibleInfo {
4563-
reason: fcw!(FutureReleaseError #147992),
4563+
reason: fcw!(FutureReleaseError #152822),
45644564
report_in_deps: false,
45654565
};
45664566
}

‎compiler/rustc_parse_format/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ impl<'input> Parser<'input> {
759759
return spec;
760760
};
761761

762-
spec.ty = self.string(self.input_vec_index);
762+
spec.ty = self.string(self.input_vec_index2pos(self.input_vec_index));
763763
spec.ty_span = {
764764
let end = self.input_vec_index2range(self.input_vec_index).start;
765765
Some(start..end)

‎compiler/rustc_resolve/src/lib.rs‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -818,17 +818,32 @@ impl<'ra> Module<'ra> {
818818
let mut traits = self.traits.borrow_mut(resolver.as_ref());
819819
if traits.is_none() {
820820
let mut collected_traits = Vec::new();
821-
self.for_each_child(resolver, |r, ident, _, ns, binding| {
821+
self.for_each_child(resolver, |r, ident, _, ns, mut decl| {
822822
if ns != TypeNS {
823823
return;
824824
}
825-
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = binding.res() {
826-
collected_traits.push((
827-
ident.name,
828-
binding,
829-
r.as_ref().get_module(def_id),
830-
binding.is_ambiguity_recursive(),
831-
));
825+
826+
let ambiguous = decl.is_ambiguity_recursive();
827+
let mut try_record_trait = |decl: Decl<'ra>| {
828+
if let Res::Def(DefKind::Trait | DefKind::TraitAlias, def_id) = decl.res() {
829+
collected_traits.push((
830+
ident.name,
831+
decl,
832+
r.as_ref().get_module(def_id),
833+
ambiguous,
834+
));
835+
true
836+
} else {
837+
false
838+
}
839+
};
840+
// Try to record at least one trait if the decl is ambiguous, such that we can
841+
// report the `ambiguous_glob_imported_traits` lint. Otherwise we would report an
842+
// error that the trait is not found.
843+
while !try_record_trait(decl)
844+
&& let Some((_, ambig_decl)) = decl.descent_to_ambiguity()
845+
{
846+
decl = ambig_decl;
832847
}
833848
});
834849
*traits = Some(collected_traits.into_boxed_slice());

‎src/bootstrap/src/core/build_steps/doc.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,10 @@ impl Step for UnstableBookGen {
13171317
cmd.arg(rustc_path);
13181318
cmd.arg(out);
13191319

1320+
// Running rustc requires the library path if rust.rpath = false
1321+
// or any other libraries are in a custom location.
1322+
builder.add_rustc_lib_path(self.build_compiler, &mut cmd);
1323+
13201324
cmd.run(builder);
13211325
}
13221326
}

‎src/tools/unstable-book-gen/src/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ fn collect_compiler_flags(rustc_path: impl AsRef<Path>) -> Features {
128128
rustc.env("RUSTC_BOOTSTRAP", "1");
129129
rustc.arg("-Zhelp");
130130
let output = t!(rustc.output());
131+
assert!(output.status.success(), "`rustc -Zhelp` failed: {output:?}");
131132
let help_str = t!(String::from_utf8(output.stdout));
132133
let parts = help_str.split("\n -Z").collect::<Vec<_>>();
133134
assert!(!parts[1..].is_empty(), "no -Z options were found");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![crate_type = "lib"]
2+
#![deny(malformed_diagnostic_format_literals)]
3+
4+
#[diagnostic::on_unimplemented(message = " \x00 \u{b123} \\\u{b123} {:?}")]
5+
//~^ERROR positional arguments are not permitted in diagnostic attributes [malformed_diagnostic_format_literals]
6+
//~|ERROR format specifiers are not permitted in diagnostic attributes [malformed_diagnostic_format_literals]
7+
#[diagnostic::on_unimplemented(note = "🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀{:?}")]
8+
//~^ERROR positional arguments are not permitted in diagnostic attributes [malformed_diagnostic_format_literals]
9+
//~|ERROR format specifiers are not permitted in diagnostic attributes [malformed_diagnostic_format_literals]
10+
pub trait ILoveUnicode {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: positional arguments are not permitted in diagnostic attributes
2+
--> $DIR/unicode.rs:4:70
3+
|
4+
LL | #[diagnostic::on_unimplemented(message = " \x00 \u{b123} \\u{b123} {:?}")]
5+
| ^ remove this format argument
6+
|
7+
= help: you can print empty braces by escaping them
8+
note: the lint level is defined here
9+
--> $DIR/unicode.rs:2:9
10+
|
11+
LL | #![deny(malformed_diagnostic_format_literals)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: format specifiers are not permitted in diagnostic attributes
15+
--> $DIR/unicode.rs:4:70
16+
|
17+
LL | #[diagnostic::on_unimplemented(message = " \x00 \u{b123} \\u{b123} {:?}")]
18+
| ^^ remove this format specifier
19+
20+
error: positional arguments are not permitted in diagnostic attributes
21+
--> $DIR/unicode.rs:7:53
22+
|
23+
LL | #[diagnostic::on_unimplemented(note = "🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀{:?}")]
24+
| ^ remove this format argument
25+
|
26+
= help: you can print empty braces by escaping them
27+
28+
error: format specifiers are not permitted in diagnostic attributes
29+
--> $DIR/unicode.rs:7:53
30+
|
31+
LL | #[diagnostic::on_unimplemented(note = "🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀🦀{:?}")]
32+
| ^^ remove this format specifier
33+
34+
error: aborting due to 4 previous errors
35+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ check-pass
2+
//@ edition:2018
3+
//
4+
// Tests that when the primary declaration is not a trait but the ambiguous declaration is,
5+
// the ambiguous trait is still recorded among the module's traits. This is to make sure
6+
// that we can report the `ambiguously_glob_import_trait` lint.
7+
//
8+
// Test case is from #159476.
9+
10+
mod module_1 {
11+
mod nested_1 {
12+
pub struct Foo;
13+
}
14+
15+
pub use nested_1::Foo;
16+
}
17+
18+
mod module_2 {
19+
// same name as the struct
20+
pub trait Foo: Sized {
21+
fn method(self) {}
22+
}
23+
impl Foo for i32 {}
24+
}
25+
26+
mod module_3 {
27+
mod nested_3 {
28+
use super::*;
29+
use crate::module_2::*;
30+
fn weird() {
31+
1_i32.method(); //~ WARNING Use of ambiguously glob imported trait `Foo` [ambiguous_glob_imported_traits]
32+
//~| WARNING this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
33+
}
34+
}
35+
36+
use crate::module_1::*;
37+
}
38+
39+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
warning: Use of ambiguously glob imported trait `Foo`
2+
--> $DIR/ambiguous-trait-and-struct-in-scope.rs:31:19
3+
|
4+
LL | use crate::module_2::*;
5+
| --------------- `Foo` imported ambiguously here
6+
LL | fn weird() {
7+
LL | 1_i32.method();
8+
| ^^^^^^
9+
|
10+
= help: Import `Foo` explicitly
11+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
12+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
13+
= note: `#[warn(ambiguous_glob_imported_traits)]` (part of `#[warn(future_incompatible)]`) on by default
14+
15+
warning: 1 warning emitted
16+

‎tests/ui/imports/ambiguous-trait-in-scope.stderr‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | 0u8.method1();
99
|
1010
= help: Import `Trait` explicitly
1111
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
12-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
12+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
1313
= note: `#[warn(ambiguous_glob_imported_traits)]` (part of `#[warn(future_incompatible)]`) on by default
1414

1515
error[E0599]: no method named `method2` found for type `u8` in the current scope
@@ -51,7 +51,7 @@ LL | 0u8.method2();
5151
|
5252
= help: Import `Trait` explicitly
5353
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
54-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
54+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
5555

5656
warning: Use of ambiguously glob imported trait `Trait`
5757
--> $DIR/ambiguous-trait-in-scope.rs:49:9
@@ -64,7 +64,7 @@ LL | 0u8.method1();
6464
|
6565
= help: Import `Trait` explicitly
6666
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
67-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
67+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
6868

6969
error[E0599]: no method named `method2` found for type `u8` in the current scope
7070
--> $DIR/ambiguous-trait-in-scope.rs:51:9
@@ -90,7 +90,7 @@ LL | 0u8.method1();
9090
|
9191
= help: Import `Trait` explicitly
9292
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
93-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
93+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
9494

9595
error[E0599]: no method named `method2` found for type `u8` in the current scope
9696
--> $DIR/ambiguous-trait-in-scope.rs:58:9
@@ -117,7 +117,7 @@ LL | 0u8.method1();
117117
|
118118
= help: Import `Trait` explicitly
119119
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
120-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
120+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
121121

122122
error[E0599]: no method named `method2` found for type `u8` in the current scope
123123
--> $DIR/ambiguous-trait-in-scope.rs:66:9
@@ -144,7 +144,7 @@ LL | 0u8.method1();
144144
|
145145
= help: Import `Trait` explicitly
146146
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
147-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
147+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
148148

149149
error[E0599]: no method named `method2` found for type `u8` in the current scope
150150
--> $DIR/ambiguous-trait-in-scope.rs:74:9
@@ -170,7 +170,7 @@ LL | 0u8.method1();
170170
|
171171
= help: Import `Trait` explicitly
172172
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
173-
= note: for more information, see issue #147992 <https://github.com/rust-lang/rust/issues/147992>
173+
= note: for more information, see issue #152822 <https://github.com/rust-lang/rust/issues/152822>
174174

175175
error[E0599]: no method named `method2` found for type `u8` in the current scope
176176
--> $DIR/ambiguous-trait-in-scope.rs:81:9

0 commit comments

Comments
 (0)