Skip to content

Conversation

@som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Mar 4, 2025

Extend "encoded package name" warning to file package object names.

Avoid warning about "casual" code in the empty package.

Add -Yreporter as a testing convenience.

Fixes #22670

@som-snytt
Copy link
Contributor Author

Practicing closing PRs for reworking instead of leaving them as drafts.

@som-snytt som-snytt closed this Mar 6, 2025
@som-snytt som-snytt reopened this Aug 17, 2025
@som-snytt
Copy link
Contributor Author

Related issue #22866 was scaladoc receiving names via tasty, where the package prefix and class name must be encoded (apparently because scaladoc would split on dot?). I don't remember the details, but it sounds ad hoc and perhaps wrong.

The scaladoc issue was the filename a.b.scala with name a.b and suffix .scala. (The use case was a.test.scala.) Then the package object uses a.b; the original ticket for the warning says tasty supplies an encoded package name, but compiler unencoded. Then maybe the warning for package object a.b or a-b should be guided by whether there is a corresponding package (under either name), in order to avoid warning about snippets.

A package

a.`b.c`.d

must preserve segments; the name is a.b$dotc.d.

If that isn't handled uniformly (correctly), then warning is the best effort.

@som-snytt
Copy link
Contributor Author

status quo for hyphen in package name but not in file package object name:

-- Warning: /tmp/macro.scala:2:8 -----------------------------------------------
2 |package `X-Y` // explicit package name gets a diagnostic
  |        ^^^^^
  |The package name `X-Y` will be encoded on the classpath, and can lead to undefined behaviour.
1 warning found
-- Error: /tmp/usage.scala:4:8 -------------------------------------------------
4 |val x = foo // warn
  |        ^^^
  |Failed to evaluate macro.
  |  Caused by class java.lang.ClassNotFoundException: X-Y.macro$package$

So it's sufficient to add the warning for file package object name. It's desirable not to warn for snippets.

@som-snytt som-snytt force-pushed the issue/22670-hyphenated-package branch 2 times, most recently from f916723 to 425ad15 Compare August 18, 2025 09:40
@som-snytt som-snytt force-pushed the issue/22670-hyphenated-package branch from 425ad15 to b08fba6 Compare October 21, 2025 21:24
@som-snytt
Copy link
Contributor Author

Added -Yno-reporter for convenience of SemanticbdTests, to reduce noise in the test console. Not sure if it should just use StoreReporter instead of Reporter.SilentReporter.

Added -Wconf to TestConfiguration.defaultOptions so that the new warning doesn't pop up in various tests in files with hyphens in the name. (The tests for the warning must reconfigure -Wconf to witness it.)

The rule is not to warn for package objects in the empty package, which is deemed "casual code". It's not obvious whether test code ought to meet that criterion.

@som-snytt som-snytt force-pushed the issue/22670-hyphenated-package branch from b08fba6 to 4595b63 Compare October 21, 2025 23:42
@som-snytt som-snytt marked this pull request as ready for review October 22, 2025 02:00
@Gedochao Gedochao requested review from noti0na1 and sjrd October 22, 2025 07:15
@Gedochao Gedochao added the release-notes Should be mentioned in the release notes label Oct 22, 2025
@Gedochao
Copy link
Contributor

added the release-notes label so we don't forget about mentioning -Yno-reporter, as the PR name/main desc doesn't mention it, cc @WojciechMazur

@som-snytt
Copy link
Contributor Author

som-snytt commented Oct 22, 2025

@Gedochao I'm not sure "private / forking" option needs to be advertised in release notes. Also, I was unsure whether to introduce an option that names a Reporter class (I think Scala 2 has -Xreporter), so I tried to be as private or "under the radar" as possible.

I'm available to make an adjustment per review. -Yno-reporter is not generally useful. I could change it to
-Yreporter:com.acme.MyReporter
as a way to introduce it without committing to long-term support.

Edit: that's kind of obvious, so I'll push -Yreporter before review, "for your consideration".

@Gedochao
Copy link
Contributor

I'm not sure "private / forking" option needs to be advertised in release notes.

@som-snytt I guess generally speaking, it depends, since we do many things under -Y* 😅
Still, perhaps this is a no-op then

@Gedochao Gedochao removed the release-notes Should be mentioned in the release notes label Oct 22, 2025
case CannotInstantiateQuotedTypeVarID // errorNumber: 219
case DefaultShadowsGivenID // errorNumber: 220
case RecurseWithDefaultID // errorNumber: 221
case EncodedPackageNameID // 222
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// errorNumber: 222

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was too quick to innovate. I'll revert it, but I was thinking that it would be nice if the compiler added the line comment for me, as a code action or quickfix. That is future work; and also verify that compilation does not reorder the existing enum.

Comment on lines 3757 to 3763
i"""Tooling may not cope with directories whose names do not match their package name.
|For example, `p-q` is encoded as `p$$minusq` and written that way to the file system.
|
|Package objects have names derived from their file names, so that names such as
|`myfile.test.scala` and `myfile-test.scala` will result in encoded names for package objects.
|
|The name `$name` is encoded to `${name.encode}`."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    i"""Tools may not handle directories whose names differ from their corresponding package names.
       |For example, `p-q` is encoded as `p$$minusq` when written to the file system.
       |
       |Package objects derive their names from the file names, so files such as
       |`myfile.test.scala` or `myfile-test.scala` can produce encoded names for the generated package objects.
       |
       |In this case, the name `$name` is encoded as `${name.encode}`."""

@som-snytt som-snytt force-pushed the issue/22670-hyphenated-package branch from 4595b63 to ddf613a Compare October 22, 2025 14:52
@som-snytt
Copy link
Contributor Author

Original comment:

Original ticket for warning is #14448

Doesn't yet address Cyclic macro dependencies.

Also packageObjectName is "forced" early to avoid cycles, even if there is no top-level def.

Also we write foo-bar.scala for descriptive snippets with top-level defs. Unreasonable to warn about that.

@som-snytt som-snytt force-pushed the issue/22670-hyphenated-package branch from ddf613a to eabc2cf Compare October 22, 2025 14:58
Copy link
Member

@noti0na1 noti0na1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

* be using for the package object that will wrap them.
*/
def packageObjectName(src: SourceFile): TermName =
def packageObjectName(src: SourceFile, srcPos: SrcPos)(using Context): TermName =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param appears unused

@som-snytt som-snytt force-pushed the issue/22670-hyphenated-package branch from eabc2cf to 9b88195 Compare October 22, 2025 20:42
@som-snytt som-snytt merged commit 55fcf06 into scala:main Oct 22, 2025
51 checks passed
@som-snytt som-snytt deleted the issue/22670-hyphenated-package branch October 22, 2025 23:15
@WojciechMazur WojciechMazur added this to the 3.8.0 milestone Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cyclic macro dependencies "caused" by a dash (-) in the filename?

4 participants